Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 33898 | Accepted: 8968 |
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
题意:n个城市,m条街道,城市x-城市y的道路承重w 求城市1-城市n的最大承重
样例:1-3 承重4 ,1-2-3 最大称重为min(3,5)=3 所以最大称重为4
求1~n中所有联通路上的最小边的最大值,最小生成树变形(也可最短路变形)
code:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int N=1005; #define inf 0x3f3f3f3f int n,m; int mp[N][N],vis[N],dis[N]; int prim() { int pos=1; vis[pos]=1; for(int i=1;i<=n;i++) { dis[i]=mp[pos][i]; } for(int i=1;i<n;i++) { int maxx=-1; for(int j=1;j<=n;j++) { if(!vis[j]&&dis[j]>maxx) { maxx=dis[j]; pos=j; } } vis[pos]=1; for(int j=1;j<=n;j++) { if(!vis[i]) dis[j]=max(dis[j],min(dis[pos],mp[pos][j])); } } return dis[n]; } int main() { int T,t=0; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(mp,0,sizeof(mp));//与最小生成树的初始值不同 memset(vis,0,sizeof(vis)); while(m--) { int x,y,z; scanf("%d%d%d",&x,&y,&z); if(mp[x][y]<z) mp[x][y]=mp[y][x]=z; } int ans=prim(); printf("Scenario #%d:\n%d\n\n",++t,ans); } }
NYOJ1248 海岛争霸海岛争霸
时间限制: 1000 ms | 内存限制: 65535 KB难度: 3
-
描述
-
神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等。加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王。 这是一个由海洋、岛屿和海盗组成的危险世界。杰克船长准备从自己所占领的岛屿A开始征程,逐个去占领每一个岛屿。面对危险重重的海洋与诡谲的对手,如何凭借智慧与运气,建立起一个强大的海盗帝国。杰克船长手头有一张整个海域的海图,上面详细地记录了各个海屿的位置,以及海屿之间的通航路线。但他发现,有的航海路线太危险了,杰克船长的战船很难直接通过,他必须想方设法绕道航行;还有的岛屿根本到达不了。杰克船长现在想把航行的危险程度降到最小。具体地来说,就是杰克船长提出若干个询问,他想知道从岛屿A 到岛屿B 有没有行驶航线,若有的话,所经过的航线,危险程度最小可能是多少。
-
输入
- 第1行: N M 表示有N个岛屿,M条直航路线 第2~M+1行: A B V 表示从岛屿A到岛屿B的航海路线的危险程度值为V。 接下面一行 : Q 表示询问的次数。 之后有Q个行: A B 表示询问从岛屿A 到岛屿B 所经过的航线,危险程度最小值 1<N≤100 0<M≤500 1≤ Q≤20 0 < V≤1000, 所有数据都是正整数。输入数据之间有一个空格。 输出
- 对于每个询问,输出占一行,一个整数,表示从岛屿A 到岛屿B 所经过的航线,危险程度最小值;若从岛屿A 无法到达岛屿B,则输出-1。 样例输入
-
10 8 1 2 5 1 3 2 2 3 11 2 4 6 2 4 4 6 7 10 6 10 5 10 7 2 5 2 3 1 4 3 7 6 7 8 3
样例输出
-
5 5 -1 5 -1
求连通路上最大边的最小值 和上面那个题正好相反,改下mp的初始化 dis[j]=min(dis[j],max(dis[pos],mp[pos][j]))
CODE:
#include<stdio.h> #include<string.h> #include<vector> #include<queue> #include<algorithm> using namespace std; const int N=105; #define inf 0x3f3f3f3f bool vis[N]; int n; int dis[N],mp[N][N]; void djs(int s,int t) { memset(vis,0,sizeof(vis)); int pos=s; vis[pos]=true; for(int i=1; i<=n; i++) { dis[i]=mp[pos][i]; } for(int j=1; j<=n; j++) { int minn=inf; for(int i=1; i<=n; i++) { if(!vis[i]&&dis[i]<minn) { minn=dis[i]; pos=i; } } vis[pos]=true; for(int i=1; i<=n; i++) { if(!vis[i]) dis[i]=min(dis[i],max(dis[pos],mp[pos][i])); } } if(dis[t]==inf) printf("-1\n"); else printf("%d\n",dis[t]); } int main() { int m,x,y,z,Q; scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { mp[i][j]=inf; } } while(m--) { scanf("%d%d%d",&x,&y,&z); if(mp[x][y]>z) mp[x][y]=mp[y][x]=z; } scanf("%d",&Q); while(Q--) { scanf("%d%d",&x,&y); djs(x,y); } }
求最大生成树可以将权值变成负的,求下最小生成树,结果取绝对值