(好题)2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest F Pizza Delivery

题意:给n个点m条边的有向图。每次使一条边反向,问你1到2的最短路变短,变长,还是不变。

解法:遇到这种题容易想到正向求一遍最短路d1,反向再求一遍最短路d2。纪录原图上的最短路为ans,然后分开考虑各种情况。

变短的情况:d1[y[i]]+d2[x[i]]+z[i]<ans

否则就剩下不变和变长两种情况:那么如果边(x,y)是起点到终点的最短路必须边的话,就会变长,否则会不变。

接下来的问题是  怎么求最短路的必经边?

求出原图1到2最短路图(这里要和求单源点的最短路图区别开来,单源点的最短路图使起点到所有其他点的最短路的集合),求法:如果d1[x[i]]+z[i]+d2[y[i]]==ans的话边(x[i],y[i])就在起点到终点的最短路图上。  把这个图变为无向图,用tarjan求桥。如果边(x,y)是桥的话就是必经边,否则为非必经边。

细节详见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
const int N=1e5+10;
LL n,m,ans;
LL d1[N],d2[N],x[N],y[N],z[N];

LL cnt=1,head[N],to[N<<1],nxt[N<<1],len[N<<1];
void add_edge(LL x,LL y,LL z) {
    nxt[++cnt]=head[x]; to[cnt]=y; len[cnt]=z; head[x]=cnt;
}

bool vis[N];
priority_queue<pii> q;
void Dijkstra(LL d[],LL s) {
    while (!q.empty()) q.pop();
    memset(vis,0,sizeof(vis));
    d[s]=0; q.push(make_pair(0,s));
    while (!q.empty()) {
        pii x=q.top(); q.pop();
        if (vis[x.second]) continue;
        vis[x.second]=1;
        for (LL i=head[x.second];i;i=nxt[i]) {
            LL y=to[i];
            if (d[y]>d[x.second]+len[i]) {
                d[y]=d[x.second]+len[i];
                q.push(make_pair(-d[y],y));
            }
        }
    }
}

int num,low[N],dfn[N],bridge[N];
void tarjan(int x,int in) {
    dfn[x]=low[x]=++num;
    for (int i=head[x];i;i=nxt[i]) {
        int y=to[i];
        if (!dfn[y]) {
            tarjan(y,i);
            low[x]=min(low[x],low[y]);

            if (low[y]>dfn[x])
                bridge[len[i]]=bridge[len[i^1]]=1;
        } else if (i!=(in^1))
            low[x]=min(low[x],dfn[y]);
    }
}

void getbridge() {
    cnt=1; memset(head,0,sizeof(head));
    for (int i=1;i<=m;i++)
        if (d1[x[i]]+z[i]+d2[y[i]]==ans)
            add_edge(x[i],y[i],i),add_edge(y[i],x[i],i);
    for (int i=1;i<=n;i++)
        if (!dfn[i]) tarjan(i,0);
}

int main()
{
    cin>>n>>m;
    for (int i=1;i<=m;i++) scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);

    memset(d1,0x3f,sizeof(d1)); memset(d2,0x3f,sizeof(d2));
    for (int i=1;i<=m;i++) add_edge(x[i],y[i],z[i]);
    Dijkstra(d1,1);

    cnt=1; memset(head,0,sizeof(head));
    for (int i=1;i<=m;i++) add_edge(y[i],x[i],z[i]);
    Dijkstra(d2,2);

    ans=d1[2]; getbridge();

    for (int i=1;i<=m;i++)
        if (ans>d1[y[i]]+d2[x[i]]+z[i]) puts("HAPPY");
        else if (bridge[i]) puts("SAD"); else puts("SOSO");
    return 0;
} 

原文地址:https://www.cnblogs.com/clno1/p/10846506.html

时间: 2024-10-02 23:12:33

(好题)2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest F Pizza Delivery的相关文章

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 (Gym - 102082)

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 A - Digits Are Not Just Characters 签到. B - Arithmetic Progressions 题意:从给定的集合中选出最多的数构成等差数列. 题解:数字排序后,设\(dp[i][j]\)表示等差数列最后一个数字为\(a[i]\),倒数第二个数字为\(a[j]\)的最大个数.然后对于每一位枚举 \(i\),\(lower\_bound()\)找有无合法的

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Krak&#243;w

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik's RectangleProblem B: What does the fox say?Problem C: Magical GCDProblem D: SubwayProblem E: EscapeProblem F: DraughtsProblem G: History courseProblem H: C

2018 ICPC Asia Jakarta Regional Contest

题目传送门 题号 A B C D E F G H I J K L 状态 Ο . . Ο . . . . Ο . . Ο Ο:当场 Ø:已补 .  :  待补 A. Edit Distance Thinking:kk pai爷 Code:kk 不能直接反转,比如"010101",直接反转后就变成"101010",右移一位,然后加个0就可以了. 所以要先统计01的数量,如果0大于1,就全变成1,1大于0,就全变成0(从数量上的改变就大于s/2了),相等的话,就看首位是0

2019-2020 ICPC, Asia Jakarta Regional Contest H. Twin Buildings

As you might already know, space has always been a problem in ICPC Jakarta. To cope with this, ICPC Jakarta is planning to build two new buildings. These buildings should have a shape of a rectangle of the same size. Now, their problem is to find lan

2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest

地址 Rank Solved A B C D E F G H I J K 16/160 27/130 O O O . O O ? . O . ? O: 当场通过 ?: 赛后通过 .: 尚未通过 A Secret of Chocolate Poles solved by chelly chelly's solution B Parallel Lines solved by ch ch's solution C Medical Checkup solved by chelly chelly's so

2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path

Pathfinding is a task of finding a route between two points. It often appears in many problems. For example, in a GPS navigation software where a driver can query for a suggested route, or in a robot motion planning where it should find a valid seque

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is