虽然不是求最短路,但是仍然是最短路题目,题意是要求1到N点的一条路径,由于每一段路都是双向的并且有承受能力,求一条路最小承受能力最大,其实就是之前POJ2253的翻版,一个求最大值最小,一个求最小值最大,于是只要修改最短路的更新条件就可以直接跑模板了
dij:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<vector> 4 #include<algorithm> 5 #include<queue> 6 #define min(a,b) a<b?a:b 7 const int INF=0x3f3f3f3f; 8 using namespace std; 9 typedef pair<int,int> pii; 10 11 struct cmp{ 12 bool operator()(pii a,pii b){ 13 return a.first<b.first; 14 } 15 }; 16 17 int g[1005][1005],n,m,dist[1005]; 18 19 void dij(int s,int p){ 20 int i; 21 memset(dist,-1,sizeof(dist)); 22 dist[s]=INF; 23 priority_queue<pii,vector<pii>,cmp>q; 24 q.push(make_pair(dist[s],s)); 25 while(!q.empty()){ 26 pii u=q.top(); 27 q.pop(); 28 if(u.first<dist[u.second])continue; 29 for(i=1;i<=n;i++){ 30 if(~g[u.second][i]){ 31 int j=min(u.first,g[u.second][i]); 32 if(dist[i]==-1||dist[i]<j){ 33 dist[i]=j; 34 q.push(make_pair(dist[i],i)); 35 } 36 } 37 } 38 } 39 printf("%d\n\n",dist[p]); 40 } 41 42 int main(){ 43 int t; 44 while(scanf("%d",&t)!=EOF){ 45 for(int q=1;q<=t;q++){ 46 int i; 47 memset(g,-1,sizeof(g)); 48 scanf("%d%d",&n,&m); 49 for(i=1;i<=m;i++){ 50 int x,y,v; 51 scanf("%d%d%d",&x,&y,&v); 52 if(g[x][y]<v){ 53 g[x][y]=g[y][x]=v; 54 } 55 } 56 printf("Scenario #%d:\n",q); 57 dij(1,n); 58 } 59 } 60 return 0; 61 }
spfa:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 const int MAXM=1000; 6 const int INF=0x3f3f3f3f; 7 8 int g[MAXM+5][MAXM+5],dist[MAXM+5],n,m; 9 bool vis[MAXM+5]; 10 11 void spfa(int s,int p){ 12 int i; 13 memset(dist,-1,sizeof(dist)); 14 dist[s]=INF; 15 memset(vis,0,sizeof(vis)); 16 queue<int>q; 17 q.push(s); 18 vis[s]=1; 19 while(!q.empty()){ 20 int u=q.front(); 21 q.pop(); 22 vis[u]=0; 23 for(i=1;i<=n;i++){ 24 int t=dist[u]<g[u][i]?dist[u]:g[u][i]; 25 if(i!=u&&g[u][i]!=-1&&dist[i]<t){ 26 dist[i]=t; 27 if(!vis[i]){ 28 q.push(i); 29 vis[i]=1; 30 } 31 } 32 } 33 } 34 printf("%d\n\n",dist[p]); 35 } 36 37 int main(){ 38 int t; 39 while(scanf("%d",&t)!=EOF){ 40 for(int q=1;q<=t;q++){ 41 int i; 42 scanf("%d%d",&n,&m); 43 memset(g,-1,sizeof(g)); 44 for(i=1;i<=m;i++){ 45 int a,b,v; 46 scanf("%d%d%d",&a,&b,&v); 47 if(g[a][b]<v){ 48 g[a][b]=g[b][a]=v; 49 } 50 } 51 printf("Scenario #%d:\n",q); 52 spfa(1,n); 53 } 54 } 55 return 0; 56 }