最短路+tarjan codeforces567E President and Roads
来源:互联网 发布:刷机备份游戏数据 编辑:程序博客网 时间:2024/05/20 02:24
这题出的非常有意义。
题意:告诉点一个有向图,和起点s终点t。保证s能到达t。
然后询问所有的边
如果这条边是s到t的最短路中不能缺少的,就输出YES
否则,可以将这条边的长度减小,但是最多中i能减小到1。此时最短路如果必经这条边就输出CAN 减少的长度
否则,就输出NO
思路:
先从s出发做一次最短路,保存在d1数组中。然后把边全部反转,从t出发做一次最短路,保存在d2数组中
如果d1[u]+w==d1[v]且d2[v]+w==d2[u],那么就说明这条边肯定是某条最短路的一部分,,但是可能并不是不能缺少的
其实我们仔细思考一下,不能缺少,这句话的含义,,缺少了,就构成不了最短路,说白了,缺少了,就不会构成连通图了
所以,这条路肯定就是桥!所以,我们把这些边记录下来,求一次tarjan,得到哪些边是桥
然后如果是桥就输出YES
如果不是桥,就去比较d1[u]和d2[v]与w和d1[t]也就是最短路之间的关系,然后得出要减少的长度输出即可
总的来说,有几个很关键的地方。
1.从起点和终点同时做最短路,,这种思维在对于起点和终点是固定的最短路都值得深思
2.d1[u]+w==d1[v]且d2[v]+w==d2[u]能说明这条路一定是某条最短路的一部分
3.不可缺少,表示这条路必然是桥
4.tarjan算法中,from一般表示的是上一个节点,但是我的代码中的from表示的是边的编号。因为这题中有重边,如果from是上一个节点就无法处理重边,(u,v)和(v,u)这一对边对应了一个编号,只要记录了from的编号,就足够防止走反向边了,,这样就完美的解决了重边的问题~
#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<map>#include<set>#include<string>#include<iostream>#include<functional>#include<algorithm>using namespace std;typedef long long LL;typedef pair<LL, int> PLI;const int MX = 2e5 + 5;const int INF = 0x3f3f3f3f;LL d1[MX], d2[MX];int Head[MX], Next[MX], rear;int Low[MX], DFN[MX], dfs_clock;struct Edge { int u, v, cost, sign;} E[MX], S[MX];void edge_init() { rear = 0; memset(Head, -1, sizeof(Head));}void edge_add(int u, int v, int cost, int sign = 0) { E[rear].u = u; E[rear].v = v; E[rear].sign = sign; E[rear].cost = cost; Next[rear] = Head[u]; Head[u] = rear++;}void dijistra(int Begin, LL *d) { memset(d, INF, sizeof(LL)*MX); d[Begin] = 0; priority_queue<PLI, vector<PLI>, greater<PLI> >work; work.push(PLI(0, Begin)); while(!work.empty()) { PLI f = work.top(); work.pop(); LL dist = f.first; int u = f.second; for(int id = Head[u]; ~id; id = Next[id]) { int cost = E[id].cost, v = E[id].v; if(dist + cost < d[v]) { d[v] = dist + cost; work.push(PLI(dist + cost, v)); } } }}void tarjan_init() { dfs_clock = 0; memset(DFN, 0, sizeof(DFN));}int tarjan(int u, int from) { Low[u] = DFN[u] = ++dfs_clock; for(int id = Head[u]; ~id; id = Next[id]) { int v = E[id].v; if(!DFN[v]) { int lowv = tarjan(v, E[id].sign); Low[u] = min(Low[u], lowv); if(lowv > DFN[u]) { S[E[id].sign].sign = 1; } } else if(E[id].sign != from && DFN[v] < DFN[u]) { Low[u] = min(Low[u], DFN[v]); } } return Low[u];}int main() { int n, m, s, t; scanf("%d%d%d%d", &n, &m, &s, &t); edge_init(); for(int i = 1; i <= m; i++) { scanf("%d%d%d", &S[i].u, &S[i].v, &S[i].cost); edge_add(S[i].v, S[i].u, S[i].cost); } dijistra(t, d2); edge_init(); for(int i = 1; i <= m; i++) { edge_add(S[i].u, S[i].v, S[i].cost); } dijistra(s, d1); edge_init(); for(int i = 1; i <= m; i++) { if(d1[S[i].u] + S[i].cost == d1[S[i].v] && d2[S[i].v] + S[i].cost == d2[S[i].u]) { edge_add(S[i].u, S[i].v, S[i].cost, i); edge_add(S[i].v, S[i].u, S[i].cost, i); } } tarjan_init(); tarjan(s, -1); for(int i = 1; i <= m; i++) { if(S[i].sign == 1) { printf("YES\n"); } else { LL ans = d1[t] - d1[S[i].u] - d2[S[i].v] - 1; if(ans > 0) printf("CAN %d\n", S[i].cost - ans); else printf("NO\n"); } } return 0;}
0 0
- 最短路+tarjan codeforces567E President and Roads
- codeforces567E. President and Roads
- CodeForces 567E President and Roads(最短路 + tarjan)
- CodeForces 567E President and Roads(最短路 + tarjan求桥)
- Codeforces 567E President and Roads (最短路 好题)
- Codeforces 567E President and Roads 【最短路】
- Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)
- Codeforces Round #Pi (Div. 2) E. President and Roads(边双无向图缩点(有重边)+最短路)
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路,桥
- 08/13 D -> codeforces Round #Pi div 2 E. President and Roads 最短路变形
- Codeforces 567E President and Roads (用Dijkstra求最短路条数 + 强连通桥)
- CodeForces 567E President and Roads(最短路 + 双联通分量)
- E. President and Roads (CF Round #Pi (Div. 2)) 优先队列的Dij+Tarjan找桥
- Gym - 100338C Important Roads 最短路+tarjan
- Codeforces 567 E. President and Roads
- Codeforces 567E President and Roads
- codeforces 567E President and Roads Dijikstra
- Codeforces 567E President and Roads 题解
- 【Obective-C编程开发】Objective-C之BOOL一清二楚
- C语言中关于.h文件和.c文件详细解析
- codeforces535C:Tavas and Karafs(二分)
- C.Stripe(Codeforces Beta Round #18 (Div. 2 Only))
- android服务的总结
- 最短路+tarjan codeforces567E President and Roads
- 自学perl 系列1
- POJ 3278 Catch that cow
- hdu 1253 胜利大逃亡
- Socket理解
- UVA 1423Guess 【拓扑排序】
- 线程池 c 实现
- Screen Property - 2
- 数据结构学习---堆栈的动态数组实现及链表实现