POJ - 1797 Heavy Transportation(最短路变形,SPFA,Dijkstra)
2017-07-26 11:23
435 查看
Heavy Transportation
Description
Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed
on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's
place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing
of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for
the scenario with a blank line.
Sample Input
Sample Output
解题思路:最短路变形题,求两点之间经过的路的最大值。普通的最短路是求最短的路径的和,因此只要把求和改成求最大即可。详细见代码!
Dijkstra邻接矩阵法
SPFA邻接矩阵
Description
Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed
on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's
place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing
of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for
the scenario with a blank line.
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
解题思路:最短路变形题,求两点之间经过的路的最大值。普通的最短路是求最短的路径的和,因此只要把求和改成求最大即可。详细见代码!
Dijkstra邻接矩阵法
#include<iostream> #include<deque> #include<memory.h> #include<stdio.h> #include<string> #include<algorithm> #include<vector> #include<math.h> #include<stack> #include<queue> #include<set> #define inf 1<<29 #define MAXV 1005 using namespace std; #define MAX 0x3f3f3f3f int rect[MAXV][MAXV]; int n,m; int tt; void dijkstra(int s,int e){ int i,j,f,v; //之前保存的是最短路径,现在保存的是最窄的路的宽度 int d[MAXV]; bool vis[MAXV]; for(i=1;i<=n;i++){ vis[i]=0; d[i]=rect[s][i]; } for(i=1;i<=n;i++){ //之前是取出路径最小的点来更新,现在是取出当前最宽的点,这里要稍作思考。 f=-1; for(j=1;j<=n;j++) if(!vis[j]&&d[j]>f){ v=j; f=d[j]; } vis[v]=1; for(j=1;j<=n;j++) //之前是更新为更短的路径,现在是更新为更窄的路,就看看当前的窄,还是新加入的窄。 if(!vis[j]&&d[j]<min(d[v],rect[v][j])) d[j]=min(d[v],rect[v][j]); } printf("Scenario #%d:\n",tt++); printf("%d\n\n",d[e]); } int main() { tt=1; int t; scanf("%d",&t); int a,b,c; while(t--){ //之前是初始化为最大,这里因为要求最宽的路,因此初始化为0 memset(rect,0,sizeof(rect)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); rect[a][b]=c; rect[b][a]=c; } dijkstra(1,n); } return 0; }
SPFA邻接矩阵
#include<iostream> #include<deque> #include<memory.h> #include<stdio.h> #include<string> #include<algorithm> #include<vector> #include<math.h> #include<stack> #include<queue> #include<set> #define inf 1<<29 #define MAXV 1005 using namespace std; #define MAX 0x3f3f3f3f int rect[MAXV][MAXV]; int n,m; int tt; int time[MAXV]; int d[MAXV]; int vis[MAXV]; bool spfa(int s,int e){ queue<int> que; memset(time,0,sizeof(time)); for(int i=1;i<=n;i++)d[i]=0; memset(vis,0,sizeof(vis)); que.push(e); d[e]=MAX;//初始化为最宽 time[e]++; vis[e]=1; while(!que.empty()){ int tp=que.front(); que.pop(); vis[tp]=0; for(int i=1;i<=n;i++){ //同上描述,看看有没有更窄的 if(d[i]<min(d[tp],rect[tp][i])){ d[i]=min(d[tp],rect[tp][i]); if(!vis[i]){ que.push(i); vis[i]=1; if(++time[i]>=n) return 1; } } } } printf("Scenario #%d:\n%d\n\n", tt++, d[s]); return 0; } int main() { tt=1; int t; scanf("%d",&t); int a,b,c; while(t--){ memset(rect,0,sizeof(rect)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); rect[a][b]=c; rect[b][a]=c; } spfa(1,n); } return 0; }
相关文章推荐
- POJ 1797 Heavy Transportation【Dijkstra最短路变形】
- POJ 2263 Heavy Cargo (SPFA+Dijkstra,最短路变形)
- POJ 2263 Heavy Cargo (SPFA+Dijkstra,最短路变形)
- POJ3635 周游诸城:变形的SPFA/Dijkstra最短路+动规思想+Heap
- 【最短路入门专题1】E - Heavy Transportation Poj 1797【Dijkstra变形题】
- POJ3635 周游诸城:变形的SPFA/Dijkstra最短路+动规思想+Heap
- poj1797 - Heavy Transportation(最大边,最短路变形spfa)
- poj 1797 最短路变形dijkstra
- POJ 1797 Heavy Transportation&&POJ 2253 Frogger 最短路 dijkstra变形
- POJ 1797 Heavy Transportation&&POJ 2253 Frogger 最短路 dijkstra变形
- 【最短路】变形-- nyoj 1248 海岛争霸、poj 1797 Heavy Transportation
- POJ 1797 Heavy Transportation(Dijkstra的稍加变形,三角形3边关系)
- poj 1797 最短路变形
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
- HDOJ 1596 find the safest road ((最短路变形) Dijkstra && SPFA)
- 图论最短路dijkstra----poj1797模板题
- POJ-1797(最短路的变形)
- Poj 1797 Heavy Transportation【Dijkstra变形】
- POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)
- POJ 1797 Heavy Transportation dijkstra 变形