POJ 1797 Heavy Transportation(最大生成树/最短路变形)
Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 31882 | Accepted: 8445 |
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.
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
解题思路
题意:找一条从 1 到 n 的道路,使得这条道路的每段距离中的最小值最大。
思路:
利用Kruskal的思想,把每段道路的权值按照从大到小排序,然后依次加边直至 1 可达 n为止,此时这条路中最小值即为答案。
利用dijkstra或是spfa的思想去解,dis[ x ]表示从 1 到达 x 的道路中,其道路段的最小值的最大值。
Kruskal()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1005; struct Edge{ int u,v,w; }edge[maxn*maxn/2]; int fa[maxn]; bool cmp(Edge a,Edge b) { return a.w > b.w; } int find( int x) { return fa[x] == x?fa[x]:fa[x] = find(fa[x]); } void Union( int x, int y) { int fx = find(x),fy = find(y); if (fx != fy) fa[fx] = fy; } int main() { int tcase; scanf ( "%d" ,&tcase); for ( int t = 1;t <= tcase;t++) { int n,m,res = 0x3f3f3f3f; scanf ( "%d%d" ,&n,&m); for ( int i = 0;i <= n;i++) fa[i] = i; for ( int i = 0;i < m;i++) { scanf ( "%d%d%d" ,&edge[i].u,&edge[i].v,&edge[i].w); } sort(edge,edge + m,cmp); for ( int i = 0; i< m;i++) { Union(edge[i].u,edge[i].v); if (find(1) == find(n)) { res = edge[i].w; break ; } } printf ( "Scenario #%d:\n" ,t); printf ( "%d\n" ,res); if (t != tcase) printf ( "\n" ); } return 0; } |
dijkstra()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int maxn = 1005; const int INF = 0x3f3f3f3f; struct Edge{ int u,v,w,nxt; bool operator < ( const Edge &a) const { return w < a.w; } }edge[maxn*maxn]; int tot = 0,head[maxn],dis[maxn]; bool vis[maxn]; void addedge( int u, int v, int w) { edge[tot] = (Edge){u,v,w,head[u] }; head[u] = tot++; } void dijkstra( int st, int n) { Edge p; priority_queue<Edge>que; memset (dis,0, sizeof (dis)); memset (vis, false , sizeof (vis)); p.v = st; p.w = INF; dis[st] = INF; que.push(p); while (!que.empty()) { p = que.top(); que.pop(); int u = p.v; if (vis[u]) continue ; vis[u] = true ; for ( int i = head[u];~i;i = edge[i].nxt) { int v = edge[i].v,w = edge[i].w; if (dis[v] < min(dis[u],w)) { dis[v] = min(dis[u],w); p.v = v,p.w = dis[v]; que.push(p); } } } } int main() { int tcase; scanf ( "%d" ,&tcase); for ( int t = 1;t <= tcase;t++) { int n,m,u,v,w; memset (head,-1, sizeof (head)); scanf ( "%d%d" ,&n,&m); for ( int i = 0;i < m;i++) { scanf ( "%d%d%d" ,&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } dijkstra(1,n); printf ( "Scenario #%d:\n" ,t); printf ( "%d\n" ,dis[n]); if (t != tcase) printf ( "\n" ); } return 0; } |
spfa()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int maxn = 1005; const int INF = 0x3f3f3f3f; struct Edge{ int u,v,w,nxt; }edge[maxn*maxn]; int tot = 0,head[maxn],dis[maxn]; bool vis[maxn]; void addedge( int u, int v, int w) { edge[tot] = (Edge){u,v,w,head[u] }; head[u] = tot++; } void spfa( int st, int ed) { memset (vis, false , sizeof (vis)); memset (dis,0, sizeof (dis)); queue< int >que; dis[st] = INF; que.push(st); vis[st] = true ; while (!que.empty()) { int u = que.front(); que.pop(); vis[u] = false ; for ( int i = head[u];~i;i = edge[i].nxt) { int v = edge[i].v,w = edge[i].w; if (min(dis[u],w) > dis[v]) { dis[v] = min(dis[u],w); if (!vis[v]) { que.push(v); vis[v] = true ; } } } } } int main() { int tcase; scanf ( "%d" ,&tcase); for ( int t = 1;t <= tcase;t++) { int n,m,u,v,w; memset (head,-1, sizeof (head)); scanf ( "%d%d" ,&n,&m); for ( int i = 0;i < m;i++) { scanf ( "%d%d%d" ,&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } spfa(1,n); printf ( "Scenario #%d:\n" ,t); printf ( "%d\n" ,dis[n]); if (t != tcase) printf ( "\n" ); } } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)