题目链接:https://www.patest.cn/contests/gplt/L2-001
好气,最后4分怎么也得不上,还没找出bug,此代码还不完全正确,明天再找,好长时间不打感觉好生疏。
思路关键在求最短路径的条数,一般我是跑两遍BFS来做。感觉通俗一些。
-----越写越丑-----
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <cstring> 6 using namespace std; 7 const int maxn = 505; 8 const int inf = 0x3f3f3f3f; 9 int w[maxn]; 10 int g[maxn][maxn]; 11 int vis[maxn]; 12 int dis[maxn]; 13 int pre[maxn]; //父亲节点 14 int cnt[maxn]; //路径数量 15 int sum[maxn]; //救援队数量 16 int n,m,s,d; 17 18 void bfs1(int s) 19 { 20 queue<int> q; 21 memset(vis,0,sizeof(vis)); 22 memset(dis,inf,sizeof(dis)); 23 vis[s] = 1; 24 dis[s] = 0; 25 q.push(s); 26 while(!q.empty()) 27 { 28 int cur = q.front();q.pop(); 29 for(int i=0;i<n;i++) 30 { 31 if(cur==i) continue; 32 if(!vis[i]&&g[cur][i]!=-1) 33 { 34 if(dis[i]>dis[cur]+g[cur][i]) 35 { 36 vis[i] = 1; 37 dis[i] = dis[cur]+g[cur][i]; 38 q.push(i); 39 } 40 else if(dis[i]==dis[cur]+g[cur][i]) 41 { 42 vis[i] = 1; 43 q.push(i); 44 } 45 } 46 } 47 } 48 } 49 void bfs2(int d) 50 { 51 queue<int> q; 52 memset(vis,0,sizeof(vis)); 53 vis[d] = 1; 54 q.push(d); 55 cnt[d] = 1; 56 sum[d] = w[d]; 57 while(!q.empty()) 58 { 59 int cur = q.front();q.pop(); 60 for(int i=0;i<n;i++) 61 { 62 if(cur==i) continue; 63 if(g[i][cur]==-1) continue; 64 if(dis[i]+g[i][cur]==dis[cur]) 65 { 66 cnt[i] += cnt[cur]; 67 if(sum[i]<sum[cur]+w[i]) 68 { 69 sum[i] = sum[cur]+w[i]; 70 pre[i] = cur; 71 } 72 if(vis[i]) continue; 73 vis[i] = 1; 74 q.push(i); 75 } 76 } 77 } 78 } 79 80 void PrintPath(int s) 81 { 82 printf("%d",s); 83 if(pre[s]==-1) 84 return; 85 printf(" "); 86 PrintPath(pre[s]); 87 } 88 int main() 89 { 90 scanf("%d %d %d %d",&n,&m,&s,&d); 91 memset(pre,-1,sizeof(pre)); 92 memset(g,-1,sizeof(g)); 93 memset(cnt,0,sizeof(cnt)); 94 memset(sum,0,sizeof(sum)); 95 for(int i=0;i<n;i++) 96 { 97 scanf("%d",&w[i]); 98 } 99 for(int i=0;i<m;i++) 100 { 101 int x,y,c; 102 scanf("%d %d %d",&x,&y,&c); 103 104 if(g[x][y]==-1) g[x][y] = c; 105 else g[x][y] = min(g[x][y],c); 106 g[y][x] = g[x][y]; 107 } 108 bfs1(s); 109 bfs2(d); 110 printf("%d %d ",cnt[s],sum[s]); 111 PrintPath(s); 112 printf(" "); 113 return 0; 114 } 115 /* 116 2 1 1 0 117 10 20 118 0 1 1 119 */