codeforces 567 E. President and Roads 【 最短路 桥 】

给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0)

正反向建图,分别求出起点到每个点的最短距离,终点到每个点的最短距离(用这个可以算出减小的边权)

再将在最短路径上的边重新建图。求出里面的桥,就是必须经过的边

wa了一上午------呜呜呜呜

先wa 19  是因为求桥的时候是无向图,数组开小了一半

然后 wa 46 ,是因为dis[]数组初始化为 1 << 30 -1 ,应该再开大点 ,开成 1 << 50 -1

我写成 1 LL * 50 -----一直wa 19------

5555555555555555555--------sad-------

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <vector>
  6 #include <map>
  7 #include <set>
  8 #include <stack>
  9 #include <queue>
 10 #include <iostream>
 11 #include <algorithm>
 12 using namespace std;
 13 #define lp (p << 1)
 14 #define rp (p << 1|1)
 15 #define getmid(l,r) (l + (r - l) / 2)
 16 #define MP(a,b) make_pair(a,b)
 17 typedef long long ll;
 18 typedef unsigned long long ull;
 19 typedef pair<int,int> pii;
 20 const int INF = (1 << 30) - 1;
 21 const int maxn = 100055;
 22
 23 int N,M;
 24 ll path;
 25 int first1[maxn],nxt1[10 * maxn],ecnt1;
 26 int first2[maxn],nxt2[10 *maxn],ecnt2;
 27 int first[maxn],nxt[10*maxn],ecnt;
 28 ll dis1[maxn],dis2[maxn];
 29
 30 int bg[20*maxn],vis[20*maxn],low[maxn],dfn[maxn];
 31 int ans[20*maxn],res[maxn];
 32 int tot,num;
 33
 34 struct edge{
 35     int v,u,cost;
 36     int tag;
 37     int ge;
 38     int id;
 39     friend bool operator < (edge a,edge b){
 40         return a.cost > b.cost;
 41     }
 42 };
 43
 44 edge e1[5*maxn],e2[5*maxn],e[5*maxn];
 45
 46 void init(){
 47     ecnt1 = ecnt2 = ecnt = 0;
 48     memset(first1,-1,sizeof(first1));
 49     memset(first2,-1,sizeof(first2));
 50     memset(first,-1,sizeof(first));
 51 }
 52
 53 void Add_edge1(int u,int v,int c){
 54     nxt1[++ecnt1] = first1[u];
 55     e1[ecnt1].u = u;
 56     e1[ecnt1].v = v;
 57     e1[ecnt1].cost = c;
 58     e1[ecnt1].tag = 0;
 59     e1[ecnt1].ge = 0;
 60     first1[u] = ecnt1;
 61 }
 62
 63 void Add_edge2(int u,int v,int c){
 64     nxt2[++ecnt2] = first2[u];
 65     e2[ecnt2].v = v;
 66     e2[ecnt2].cost = c;
 67     e2[ecnt2].tag = 0;
 68     e2[ecnt2].ge = 0;
 69     first2[u] = ecnt2;
 70 }
 71
 72 void Add_edge(int u,int v,int c){
 73     nxt[ecnt] = first[u];
 74     e[ecnt].v = v;
 75     e[ecnt].u = u;
 76     e[ecnt].id = c;
 77     first[u] = ecnt++;
 78
 79     nxt[ecnt] = first[v];
 80     e[ecnt].v = u;
 81     e[ecnt].u = v;
 82     e[ecnt].id = c;
 83     first[v] = ecnt++;
 84 }
 85
 86 struct cmp{
 87     bool operator ()(pii a,pii b){
 88         return a.first > b.first;
 89     }
 90 };
 91
 92 void Dijstra1(int s){
 93     priority_queue<pii,vector<pii >,cmp> PQ;
 94     dis1[s] = 0;
 95     PQ.push(MP(dis1[s],s));
 96     int cnt = 0;
 97     while(!PQ.empty()){
 98         pii x = PQ.top(); PQ.pop();
 99         if(dis1[x.second] < x.first) continue;
100         for(int i = first1[x.second]; i != -1; i = nxt1[i]){
101             int v = e1[i].v;
102             if(dis1[v] > dis1[x.second] + e1[i].cost){
103                 dis1[v] = dis1[x.second] + e1[i].cost;
104                 PQ.push(MP(dis1[v],v));
105             }
106         }
107     }
108 }
109
110 void Dijstra2(int s){
111     priority_queue<pii,vector<pii >,cmp> PQ;
112     dis2[s] = 0;
113     PQ.push(MP(dis2[s],s));
114     int cnt = 0;
115     while(!PQ.empty()){
116         pii x = PQ.top(); PQ.pop();
117         if(dis2[x.second] < x.first) continue;
118         for(int i = first2[x.second]; i != -1; i = nxt2[i]){
119             int v = e2[i].v;
120             if(dis2[v] > dis2[x.second] + e2[i].cost){
121                 dis2[v] = dis2[x.second] + e2[i].cost;
122                 PQ.push(MP(dis2[v],v));
123             }
124         }
125     }
126 }
127
128
129 void Dfs(int p,int pre){
130     dfn[p] = low[p] = ++tot;
131     for(int i = first[p]; ~i; i = nxt[i]){
132         int v = e[i].v;
133         if(vis[i]) continue;
134         vis[i] = vis[i ^ 1] = true;
135         if(!dfn[v]){
136             Dfs(v,p);
137             low[p] = min(low[p],low[v]);
138             if(low[v] > dfn[p]) {
139                 bg[i] = bg[i ^ 1] = true;
140                 ans[e[i].id] = 1;
141             }
142         }
143         else low[p] = min(low[p],dfn[v]);
144     }
145 }
146
147 void Tarjan(){
148     memset(dfn,0,sizeof(dfn));
149     memset(low,0,sizeof(low));
150     memset(bg,false,sizeof(bg));
151     memset(vis,false,sizeof(vis));
152     tot = 0;
153     for(int i = 1; i <= N; ++i) if( dfn[i] == 0) Dfs(i,-1);
154 }
155
156 void solve(){
157     memset(res,-1,sizeof(res));
158     for(int i = 1;i <= M;i++){
159         int u = e1[i].u;
160         int v = e1[i].v;
161         if(ans[i] == 1) {
162             res[i] = 0;
163             continue;
164         }
165         ll need = path - dis1[u] - dis2[v];
166         if(need > 1){
167              res[i] = e1[i].cost - need + 1;
168         }
169     }
170
171
172     for(int i = 1;i <= M;i++){
173         if(res[i] == 0) puts("YES");
174         else if(res[i] == -1) puts("NO");
175         else printf("CAN %d\n",res[i]);
176     }
177 }
178
179 int main(){
180     int a,b,c,s,t;
181     num = 0;
182     while(scanf("%d%d%d%d",&N,&M,&s,&t) != EOF){
183         num++;
184         init();
185         int x;
186
187         for(int i = 1; i <= M; ++i){
188             scanf("%d%d%d",&a,&b,&c);
189             Add_edge1(a,b,c);
190             Add_edge2(b,a,c);
191         }
192
193         for(int i = 1;i <= N;i++) dis1[i] = dis2[i] = 1ll << 50;
194
195         Dijstra1(s);
196         Dijstra2(t);
197         path = dis1[t];
198
199         for(int u = 1;u <= N;u++){
200             for(int i = first1[u];~i;i = nxt1[i]){
201                 int v = e1[i].v;
202                 if(dis1[u] + dis2[v] + e1[i].cost == path) {
203                     e1[i].tag = 1;
204                     e2[i].tag = 1;
205                     Add_edge(u,v,i);
206                 }
207             }
208         }
209
210         memset(ans,0,sizeof(ans));
211         Tarjan();
212         solve();
213     }
214     return 0;
215 }

时间: 2024-08-05 17:33:05

codeforces 567 E. President and Roads 【 最短路 桥 】的相关文章

【CodeForces 567E】President and Roads(最短路)

Description Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year t

Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)

题目地址:codeforces #pi (DIV2) E 题目很水..就是先求两边最短路,然后把可能为最短路的边挑出来,然后判断是否yes只需要转化成无向图跑一遍tarjan,找出割边,割边就是yes,然后剩下的边就让它的值为最短路-1就行了,如果-1后变成了非正数,就是no. 但是!!!居然卡spfa!!那是不是说cf以后就不能用可以卡的算法了..完全可以出组数据来卡这些算法...比如spfa,isap... 于是为了这题,又看了一遍迪杰斯特拉算法.. 代码如下: #include <cstd

CodeForces 567E President and Roads(最短路 + tarjan)

CodeForces 567E President and Roads Description Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s?≠?t). The cities are connected by one-way roads, the travel time for each of the road

Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路

#include<time.h> #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #in

Codeforces 543B Destroying Roads(最短路)

题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1:假设最后留下的边是互不相交的两条路径.此时的答案是n-s1到t1的最短路径-s2到t2的最短路径. 2:假设最后留下的边有重合的一段,此时只要枚举重合的这一段的起点和终点,就可以判断.注意此时要考虑这两条路径经过枚举的两点的顺序. 限制的条件比较多,可以用来剪枝的条件也很多. 由于所有边的长度为1,用DF

Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路 删边

题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <stack> #in

ACM学习历程—CodeForces 601A The Two Routes(最短路)

题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没有陆路. 这种情况下就会有一个结论,就是至少有一种交通可以直接1到n. 这样只需要对另一种跑一个最短路就OK了. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath&

【poj 1724】 ROADS 最短路(dijkstra+优先队列)

ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N cities named with numbers 1 - N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want