题目链接:
题目大意:给出一个N个顶点M条边的有向图。顶点编号1~N。给出K个双向路,问选择其中一条路能使s到t的距离最短为多少。如果无合法选择,输出-1. M < 100000 ,任意两点距离 < 1000。
解题思路:一个原图,一个反向图,两次dijkstra记录s到每个点以及t到每个点的距离。之后对于每个双向路判断一下s->u->v->t, t->u->v->s与原s->t大小即可。
代码:
1 const int maxn = 2e5 + 5; 2 struct Edge{ 3 int to, next, val; 4 }; 5 Edge edges[maxn], edges1[maxn]; 6 int tot, head[maxn], tot1, head1[maxn]; 7 int n, m, k, s, t; 8 bool vis[maxn]; 9 priority_queue, greater > q;10 int dis[maxn], dis1[maxn];11 12 void init(){13 tot = 0, tot1 = 0;14 memset(head, -1, sizeof(head));15 memset(head1, -1, sizeof(head1));16 }17 void addEdge(int u, int v, int w){18 edges[tot].to = v;19 edges[tot].val = w;20 edges[tot].next = head[u];21 head[u] = tot++;22 }23 void addEdge1(int u, int v, int w){24 edges1[tot].to = v;25 edges1[tot].val = w;26 edges1[tot].next = head1[u];27 head1[u] = tot++;28 }29 void dijk(){30 while(!q.empty()) q.pop();31 memset(dis, 0x3f, sizeof(dis));32 memset(vis, 0, sizeof(vis));33 dis[s] = 0;34 q.push(PII(0, s));35 while(!q.empty()){36 int u = q.top().second; q.pop();37 if(vis[u]) continue;38 vis[u] = 1;39 for(int i = head[u]; i != -1; i = edges[i].next){40 int v = edges[i].to, w = edges[i].val;41 if(dis[v] > dis[u] + w){42 dis[v] = dis[u] + w;43 q.push(PII(dis[v], v));44 }45 }46 }47 return;48 }49 void dijk1(){50 while(!q.empty()) q.pop();51 memset(dis1, 0x3f, sizeof(dis1));52 memset(vis, 0, sizeof(vis));53 dis1[t] = 0;54 q.push(PII(0, t));55 while(!q.empty()){56 int u = q.top().second; q.pop();57 if(vis[u]) continue;58 vis[u] = 1;59 for(int i = head1[u]; i != -1; i = edges1[i].next){60 int v = edges1[i].to, w = edges1[i].val;61 if(!vis[v] && dis1[v] > dis1[u] + w){62 dis1[v] = dis1[u] + w;63 q.push(PII(dis1[v], v));64 }65 }66 }67 return;68 }69 int main(){70 int ca;71 scanf("%d", &ca);72 while(ca--){73 init();74 scanf("%d %d %d %d %d", &n, &m, &k, &s, &t);75 for(int i = 0; i < m; i++){76 int u, v, w;77 scanf("%d %d %d", &u, &v, &w);78 addEdge(u, v, w);79 addEdge1(v, u, w);80 }81 dijk(); dijk1();82 int ans = dis[t];83 for(int i = 0; i < k; i++){84 int u, v, w;85 scanf("%d %d %d", &u, &v, &w);86 int tp1 = dis[u] + w + dis1[v], tp2 = dis[v] + w + dis1[u];87 if(tp1 > 0) ans = min(ans, tp1); 88 if(tp2 > 0) ans = min(ans, tp2);89 }90 if(ans != inf) printf("%d\n", ans);91 else puts("-1");92 }93 }
题目:
TRAFFICN - Traffic Network
The city traffic network consists of n nodes numbered from 1 to n and m one-way roads connecting pairs of nodes. In order to reduce the length of the shortest path between two different critical nodes s and t, a list of k two-way roads are proposed as candidates to be constructed. Your task is to write a program to choose one two-way road from the proposed list in order to minimize the resulting shortest path between s and t.
Input
The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each data set, the first line contains five positive integers n (n ≤ 10 000), m (m ≤ 100 000), k (k < 300), s (1 ≤ s ≤ n), t (1 ≤ t ≤ n) separated by space. The ith line of the following m lines contains three integers di, ci, li separated by space, representing the length li ( 0< li ≤ 1000) of the ith one-way road connecting node di to ci. The jth line of the next k lines contains three positive integers uj, vj and qj (qj ≤ 1000) separated by space, representing the jth proposed two-way road of length qj connecting node uj to vj.
Output
For each data set, write on one line the smallest possible length of the shortest path after building the chosen one two-way road from the proposed list. In case, there does not exist a path from s to t, write -1.
Example
Sample Input14 5 3 1 41 2 132 3 193 1 253 4 174 1 181 3 232 3 52 4 25 Sample Output35