poj1797 - Heavy Transportation(最大边,最短路变形spfa)

题目大意:

给你以T, 代表T组测试数据,一个n代表有n个点, 一个m代表有m条边, 每条边有三个参数,a,b,c表示从a到b的这条路上最大的承受重量是c,

让你找出一条线路,要求这条线路上的最大的承重, 在所有其他线路最小。

题目分析:

这里只要将spfa进行一下变形就可以解决这问题了。

首先 我们的dist数组,起点位置要初始化为 INF, 其他位置初始化为 0

然后我们更新 dist 数组, 结果输出 dist[n]就行了

为什么这样写: 因为我们每次要找 所有路径中的最大边的最小一个, 说的可能有写绕口

递推式是: dist[e] = min(dist[e], max(dist[s], G[s][i]) );

下面是代码:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 using namespace std;
 8 #define INF 0xfffffff
 9 #define maxn 1050
10
11 struct Edge
12 {
13     int e;
14     long long w;
15 };
16 vector<Edge> G[maxn];
17 long long dist[maxn];
18 bool vis[maxn];
19 int m, n;
20 long long Spfa()
21 {
22     Edge P, Pn;
23     P.e = 1, P.w = 0;
24     queue <Edge> Q;
25     Q.push(P);
26
27     while( !Q.empty() )
28     {
29         P = Q.front();
30         Q.pop();
31         vis[P.e] = false;
32         int len = G[P.e].size();
33
34         for(int i=0; i<len; i++)
35         {
36             Pn = G[P.e][i];
37
38             if(dist[Pn.e] < min(dist[P.e],Pn.w) )
39             {
40                 dist[Pn.e] = min(dist[P.e],Pn.w);
41
42                 if(!vis[Pn.e])
43                 {
44                     vis[Pn.e] = true;
45                     Q.push(Pn);
46                 }
47             }
48         }
49     }
50     return dist[n];
51 }
52 void Init()
53 {
54     for(int i=1; i<=n ;i++)
55     {
56         G[i].clear();
57         vis[i] = false;
58         dist[i] = 0;
59     }
60     dist[1] = INF;
61 }
62 int main()
63 {
64     int T, cas = 1;
65     Edge P;
66     cin >> T;
67
68     while(T--)
69     {
70         scanf("%d%d",&n,&m);
71
72         Init();
73         for(int i=0; i<m; i++)
74         {
75             int a, b, c;
76             scanf("%d%d%d",&a,&b,&c);
77             P.e = b, P.w = c;
78
79             G[a].push_back(P);
80
81             P.e = a;
82
83             G[b].push_back(P);
84         }
85         long long ans = Spfa();
86
87         printf("Scenario #%d:\n%lld\n",cas++,ans);
88         if(T)
89             printf("\n");
90
91     }
92     return 0;
93 }
时间: 2024-10-09 06:33:11

poj1797 - Heavy Transportation(最大边,最短路变形spfa)的相关文章

Heavy Transportation POJ 1797 最短路变形

Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你求从编号为1的城市到编号为n的城市的路线中,最大能经过多重的车. 解题思路 这个题可以使用最短路的思路,不过转移方程变了\(dis[j]=max(dis[j], min(dis[u], e[u][j]))\).这里dis[j]表示从标号为1的点到达编号为j的点的路径中,最小的承重能力,就像短板效应样

POJ 1797 Heavy Transportation【Dijkstra最短路变形】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 29682   Accepted: 7919 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ-1797 Heavy Transportation(最大生成树)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 39963   Accepted: 10507 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ1797 Heavy Transportation 【Dijkstra】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted: 5569 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

poj1797 Heavy Transportation (Kruskal 算法)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 22796   Accepted: 6053 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

poj1797 Heavy Transportation(最短路变形)

题目大意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量 解题思路:其实这个求最大边可以近似于求最短路,只要修改下找最短路更新的条件就可以了 #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=1010; const int INF=0x3f3f3f3

POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

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

poj1797——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 t

POJ1797 Heavy Transportation

解题思路:典型的Kruskal,不能用floyed(会超时),上代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define inf 0x3f3f3f3f 6 const int maxn = 1005; 7 int father[maxn]; 8 9 struct node{ 10 int x, y, w; 11 }p[maxn*max