Heavy Transportation(最大生成树)
来源:互联网 发布:沉默的螺旋 案例 知乎 编辑:程序博客网 时间:2024/05/29 04:36
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.
13 31 2 31 3 42 3 5
Scenario #1:4
题意:求1到n的最优值(1到n的路径中,边权最小的定义是min,求出min的最大值)
思路:其实就是求最大生成树的最小边权;
//Kruskal 解法;
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int pre[600000];//此题易RE;struct node{int a;int b;int c;}s[600000];bool cmp(node x,node y)//根据题意,降序排列{return x.c>y.c;}int find(int x){if(pre[x]==-1)return x;return pre[x]=find(pre[x]);}int main(){int m,n,i,t,k=0;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);memset(pre,-1,sizeof(pre));for(i=0;i<m;i++)scanf("%d %d %d",&s[i].a,&s[i].b,&s[i].c);sort(s,s+m,cmp);int ans;for(i=0;i<m;i++){int fx=find(s[i].a);int fy=find(s[i].b);if(fx!=fy){pre[fy]=fx;if(find(1)==find(n)){ans=s[i].c;break;}}}printf("Scenario #%d:\n",++k);//zz博主因为没加冒号,wa了数次;printf("%d\n\n",ans);//注意空行;}return 0;}
//Dijkstra 解法;#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;int graph[1010][1010];int dij(int n){int mst[1010],lowcost[1010];int i,j,minid;memset(mst,0,sizeof(mst));for(i=1;i<=n;i++)//这个时候lowcost代表最大生成树的最小边权;lowcost[i]=graph[1][i];mst[1]=1;for(i=1;i<=n;i++){int smin=0;for(j=1;j<=n;j++){if(lowcost[j]>smin && !mst[j]){smin=lowcost[j];minid=j;}}mst[minid]=1;for(j=1;j<=n;j++){if(!mst[j] && lowcost[j]<min(graph[minid][j],lowcost[minid]))lowcost[j]=min(graph[minid][j],lowcost[minid]);}}return lowcost[n];}int main(){int t,m,n,i,j,cost,k=0;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);memset(graph,0,sizeof(graph));while(m--){scanf("%d %d %d",&i,&j,&cost);graph[i][j]=graph[j][i]=cost;//题目数据应该把i,j之间的权值就给了一次;}printf("Scenario #%d:\n",++k);printf("%d\n\n",dij(n));}return 0;}
//prim 解法; #include<stdio.h>#include<string.h>int graph[1010][1010];int prim(int n){int lowcost[1010],mst[1010];int i,j,min,minid,ans=0x3f3f3f3f;memset(mst,0,sizeof(mst));mst[1]=1;for(i=2;i<=n;i++)lowcost[i]=graph[1][i];for(i=1;i<n;i++){min=0;for(j=1;j<=n;j++){if(!mst[j] && lowcost[j]>min){min=lowcost[j];minid=j;}}if(ans>min)ans=min;if(minid==n)//注意1和n连接后,结束; return ans;mst[minid]=1;for(j=1;j<=n;j++){if(!mst[j] && graph[minid][j]>lowcost[j])lowcost[j]=graph[minid][j];}}}int main(){int m,n,i,t,j,cost,k=0;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);memset(graph,0,sizeof(graph));while(m--){scanf("%d %d %d",&i,&j,&cost);graph[i][j]=graph[j][i]=cost;}printf("Scenario #%d:\n",++k);printf("%d\n\n",prim(n));}return 0;}
- Heavy Transportation(最大生成树)
- POJ1797 Heavy Transportation(最大生成树解)
- poj 1797 Heavy Transportation(最大生成树)
- POJ - 1797 Heavy Transportation(求“最大生成树”)
- POJ 1797 Heavy Transportation(最大生成树-Prim)
- poj1797——Heavy Transportation(最大生成树)
- POJ 1797 Heavy Transportation(最大生成树)
- POJ1797-Heavy Transportation(Dijkstra 变式& 最大生成树)
- POJ 1791 Heavy Transportation(最大生成树)
- POJ 1797 Heavy Transportation(最大生成树)
- poj 1797 Heavy transportation最大生成树
- poj1797 Heavy Transportation 最大生成树
- POJ 1797 Heavy Transportation(最大生成树)
- poj 1797 Heavy Transportation 最小生成树 最大生成树
- POJ 1797 Heavy Transportation (Prime 最大生成树)
- POJ 1797 Heavy Transportation 【最大生成树,Prime】
- POJ-1797Heavy Transportation (最小生成树问题)
- (POJ 1797)Heavy Transportation 最大生成树|最短路变形 (理解最短路核心思想好题)
- 安卓自定义View进阶 - Path之完结篇(伪)
- Plugin with id 'com.neenbedankt.android-apt' not found.
- 453. Minimum Moves to Equal Array Elements | 计算步数
- div高度全屏
- Sqoop学习笔记 --- sqoop使用时候常见错误
- Heavy Transportation(最大生成树)
- 拼接字符串到url跳转页面后解析
- 4 Values whose Sum is 0 POJ - 2785(折半枚举)题解
- MeasureSpec学习 - 转
- DOM创建元素、属性、文本节点
- Hbase shell基础和常用命令详解
- 第三章 员工考勤信息管理
- PAT BASIC LEVEL 1053. 住房空置率 (20)
- 基本数据类型