poj3268 Silver Cow Party(两次dijkstra)
Posted Surprisez
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3268 Silver Cow Party(两次dijkstra)相关的知识,希望对你有一定的参考价值。
https://vjudge.net/problem/POJ-3268
一开始floyd超时了。。
对正图定点求最短,对逆图定点求最短,得到任意点到定点的往返最短路。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<stack> 8 #define lson l, m, rt<<1 9 #define rson m+1, r, rt<<1|1 10 #define INF 0x3f3f3f3f 11 typedef unsigned long long ll; 12 using namespace std; 13 int t[1010][1010], rt[1010][1010]; 14 int d[1010], rd[1010], vis[1010]; 15 int n, m, x, a, b, T; 16 void dijkstra(int dist[], int a[][1010]) 17 { 18 memset(vis, 0, sizeof(vis)); 19 for(int i = 1; i <= n; i++){ 20 dist[i] = INF; 21 } 22 dist[x] = 0; 23 for(int i = 1; i <= n; i++){ 24 int mini = INF, k = -1; 25 for(int j = 1; j <= n; j++){ 26 if(!vis[j]&&mini > dist[j]){ 27 mini = dist[j]; 28 k = j; 29 } 30 } 31 vis[k] = 1; 32 for(int j = 1; j <= n; j++){ 33 if(!vis[j]&&dist[j] > dist[k]+a[k][j]){ 34 dist[j] = dist[k]+a[k][j]; 35 } 36 } 37 } 38 } 39 int main() 40 { 41 cin >> n >> m >> x; 42 for(int i = 1; i <= n; i++){ 43 for(int j = 1; j <= n; j++){ 44 t[i][j] = INF; rt[i][j] = INF; 45 } 46 } 47 for(int i = 1; i <= m; i++){ 48 cin >> a >> b >> T; 49 t[a][b] = T;//图 50 rt[b][a] = T;//逆图 51 } 52 dijkstra(d, t); 53 dijkstra(rd, rt); 54 int maxm = -INF; 55 for(int i = 1; i <= n; i++){ 56 if(d[i]!=INF&&rd[i]!=INF&&d[i]+rd[i] > maxm){ 57 maxm = d[i]+rd[i]; 58 } 59 } 60 cout << maxm << endl; 61 return 0; 62 }
以上是关于poj3268 Silver Cow Party(两次dijkstra)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3268 Silver Cow Party (Dijkstra)