POJ 1797 Heavy Transportation(最大生成树/最短路变形)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 31882 | Accepted: 8445 |
Description
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
Output
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
解题思路
Kruskal()
#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()
#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()
#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"); } }
POJ 1797 Heavy Transportation(最大生成树/最短路变形)的更多相关文章
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- POJ - 1797 Heavy Transportation 单源最短路
思路:d(i)表示到达节点i的最大能运输的重量,转移方程d(i) = min(d(u), limit(u, i));注意优先队列应该以重量降序排序来重载小于符号. AC代码 #include < ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- POJ 1797 Heavy Transportation
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 1797 Heavy Transportation SPFA变形
原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- POJ 1797 Heavy Transportation (最短路)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 22440 Accepted: ...
随机推荐
- 基于Nuclear的Web组件-Todo的十一种写法
刀耕火种 刀耕火种是新石器时代残留的农业经营方式.又称迁移农业,为原始生荒耕作制. var TodoApp = Nuclear.create({ add: function (evt) { evt.p ...
- SDK接入(2)之Android Google Play内支付(in-app Billing)接入
SDK接入(2)之Android Google Play内支付(in-app Billing)接入 继上篇SDK接入(1)之Android Facebook SDK接入整理完Facebook接入流程之 ...
- 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下:
Asp.net MVC中的ViewData与ViewBag ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP. ...
- 企业号微信支付 公众号支付 H5调起支付API示例代码 JSSDK C# .NET
先看效果 1.本文演示的是微信[企业号]的H5页面微信支付 2.本项目基于开源微信框架WeiXinMPSDK开发:https://github.com/JeffreySu/WeiXinMPSDK 感谢 ...
- 数据库实战案例—————记一次TempDB暴增的问题排查
前言 很多时候数据库的TempDB.日志等文件的暴增可能导致磁盘空间被占满,如果日常配置不到位,往往会导致数据库故障,业务被迫中断. 这种文件暴增很难排查,经验不足的一些运维人员可能更是无法排查具体原 ...
- Masonry和FDTemplateLayoutCell 结合使用示例Demo
我们知道,界面布局可以用Storyboard或Xib结合Autolayout实现,如果用纯代码布局,比较热门的有Masonry.SDAutoLayout,下面的简单demo,采用纯代码布局,实现不定高 ...
- webdriver学习笔记
该篇文章记录本人在学习及使用webdriver做自动化测试时遇到的各种问题及解决方式,问题比较杂乱.问题的解决方式来源五花八门,如有疑问请随时指正一遍改正. 1.WebDriver入门 //webdr ...
- Warm myself by my hand
周末的尾巴了. 前几天白日里的气温降到10摄氏度以下,穿上了秋裤.隔天跑一次步,晚上九点多,5公里,25分钟左右.换上薄薄的运动裤,两件运动衣.一出宿舍门就没觉得冷,跑着跑着就愈加热了起来.遇到的问题 ...
- Spring 事务详解
实现购买股票案例: 一.引入JAR文件: 二.开始搭建分层架构---创建账户(Account)和股票(Stock)实体类 Account: ? 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- [No0000AC]全局鼠标键盘模拟器
之前网上下载的一位前辈写的工具,名叫:Dragon键盘鼠标模拟器,网址http://www.esc0.com/. 本软件能够录制键盘鼠标操作,并能按要求回放,对于重复的键盘鼠标操作,可以代替人去做,操 ...