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.
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.
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.
1
3 3
1 2 3
1 3 4
2 3 5
Scenario #1:
4
Status | Accepted |
---|---|
Time | 344ms |
Memory | 4120kB |
Length | 1398 |
Lang | C++ |
Submitted | 2017-04-29 00:51:26 |
有一个城市有若干的路,每条路有自身的最大承重限制,比如A-B-C,A-B承重2kg,B-C承重3kg故这条通路最大承重2kg
第一行是 计划个数
第二行 n m
n表示 从1要到n处去
m行数据 x处---y处 承受最大重量
数据<=10^6
同frogger ,找到1-n所有路径中的最小承载重量,在这些可行的最小承载重量中找到一条路,这条路的最小承载重量最大。
核心代码:
for(j=2;j<=n;j++)
{
length=min(weight[k],map[k][j]);
weight[j]=max(weight[j],length);
}
找到weight[k]是1-k最大承重质量,k可以到j,找到min(weight[k],map[k][j]),就是1-j的最大承重重量。
注意map数组要开的大几个数据,否则会越界。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 1007
#define INF 1<<30
int plan_num,n,m;
int weight[MAXN];
int map[MAXN][MAXN];
void input()
{
int i,j,x,y,w;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=0;
scanf("%d%d",&n,&m);
i=m;
while(i--)
{
scanf("%d%d%d",&x,&y,&w);
//cout<<x<<y<<w<<endl;
map[x][y]=map[y][x]=w;
}
}
void dijkstar()
{
int i,j,temp,k,length;
bool flag[MAXN];
memset(flag,false,sizeof(flag));
for(i=1;i<=n;i++)
weight[i]=map[1][i];
for(i=1;i<=n;i++)
{
temp=-INF;
for(j=2;j<=n;j++)
{
if(!flag[j]&&(temp<weight[j]))
{
k=j;
temp=weight[j];
}
}
flag[k]=true;
for(j=2;j<=n;j++)
{
length=min(weight[k],map[k][j]);
weight[j]=max(weight[j],length);
/*
if((!flag[j])&&(weight[j]<min(weight[k],map[k][j])))
weight[j]=min(weight[k],map[k][j]);*/
}
/*for(j=1;j<=m;j++)
cout<<weight[j]<<" ";
cout<<endl;*/
}
}
int main()
{
int i,j;
scanf("%d",&plan_num);
for(i=1;i<=plan_num;i++)
{
input();
dijkstar();
printf("Scenario #%d:\n",i);
printf("%d\n\n",weight[n]);
}
}