标签:bool ext return pac struct dde txt mes for
题目链接: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3347&konwledgeId=40
解题思路: 直接求出所有的最短路,以及最短路上的特征距离。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int inf=0x3f3f3f3f; 6 const int MAXN=100005; 7 const LL MON7 = 1e9+7; 8 9 struct Edge 10 { 11 int v,c; 12 Edge(){} 13 Edge(int v, int c):v(v),c(c){} 14 }edge[10*MAXN]; 15 int head[MAXN]; 16 int dis[MAXN]; 17 int fdis[MAXN]; 18 bool vis[MAXN]; 19 int next_edge[10*MAXN]; 20 int cnt; 21 int n,m; 22 void init() 23 { 24 memset(head,-1,sizeof(head)); 25 cnt=0; 26 } 27 void addEdge(int u,int v,int c) 28 { 29 edge[cnt]=Edge(v,c); 30 next_edge[cnt]=head[u]; 31 head[u]=cnt++; 32 } 33 void spfa(int s,int e) 34 { 35 for (int i=0;i<=n;++i) dis[i]=inf; 36 memset(fdis,0,sizeof(fdis)); 37 dis[s]=0; 38 queue<int> q; 39 q.push(s); 40 vis[s]=1; 41 while (!q.empty()) 42 { 43 int u=q.front(); 44 q.pop(); 45 vis[u]=0; 46 for (int i=head[u];i!=-1;i=next_edge[i]) 47 { 48 int v=edge[i].v; 49 int c=edge[i].c; 50 if (dis[v]>dis[u]+c) 51 { 52 dis[v]=dis[u]+c; 53 fdis[v]=max(fdis[u],c); 54 if (!vis[v]) q.push(v), vis[v]=1; 55 } else if (dis[v]==dis[u]+c && max(fdis[u],c)>fdis[v]) 56 { 57 fdis[v]=max(fdis[u],c); 58 if (!vis[v]) q.push(v), vis[v]=1; 59 } 60 } 61 } 62 if (dis[e]==inf) printf("No answer\n"); 63 else printf("%d\n",fdis[e]); 64 } 65 66 int main() 67 { 68 #ifndef ONLINE_JUDGE 69 freopen("test.txt","r",stdin); 70 #endif // ONLINE_JUDGE 71 int Case; 72 int s,t; 73 scanf("%d",&Case); 74 for (int ct=1;ct<=Case;++ct) 75 { 76 scanf("%d%d",&n,&m); 77 scanf("%d%d",&s,&t); 78 init(); 79 int u,v,c; 80 while(m--) 81 { 82 scanf("%d%d%d",&u,&v,&c); 83 addEdge(u,v,c); 84 addEdge(v,u,c); 85 } 86 printf("Case #%d: ", ct); 87 spfa(s,t); 88 } 89 return 0; 90 }
标签:bool ext return pac struct dde txt mes for
原文地址:https://www.cnblogs.com/djingjing/p/8977914.html