Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 19984 | Accepted: 5315 |
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条路,每条路有相应容量,选择起点到终点的一条路径,使得容量最小值最大。
之前处理这种问题,只会floyd。Dijkstra处理第一次。
AC代码:
#include
#include
#include
#include
#define INF 0x3f3f3f3f
using namespace std;
int dis[1010],m,n,edge[1010][1010],visited[1010];
void Dijkstra()
{
int i,j,now;
dis[1]=INF;
memset(visited,0,sizeof(visited));
for(i=1;i<=n;i++)
{
int ma=-10;
for(j=1;j<=n;j++)
{
if(!visited[j]&&dis[j]>ma) //选择当前 dis 最大值
{
ma=dis[j];
now=j;
}
}
visited[now]=1;
for(j=1;j<=n;j++)
{
if(!visited[j]&&edge[now][j]!=INF)
dis[j]=max(dis[j],min(dis[now],edge[now][j])); //与前面求得的值 比较 ,更新
}
}
}
int main()
{
int cas;
while(~scanf("%d",&cas))
{
int i,j;
for(i=1;i<=cas;i++)
{
scanf("%d",&n);
/* if(n==1) //这一段 加与不加 没关系
{
printf("Scenario #%d:\n",i);
printf("1000000\n\n");
continue;
}*/
scanf("%d",&m);
memset(edge,0x3f,sizeof(edge));
memset(dis,0,sizeof(dis));
for(j=1;j<=m;j++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
edge[x][y]=edge[y][x]=z;
}
Dijkstra();
printf("Scenario #%d:\n",i);
printf("%d\n\n",dis[n]);
}
}
return 0;
}
下面的是 邻接表实现:
#include
#include
#include
#include
#define INF 0x3f3f3f3f
using namespace std;
struct edge
{
int st,ed;
int val,next;
}a[1000000];
int head[1010],dis[1010],visited[1010],m,n;
void Dijkstra()
{
int i,j,now;
dis[1]=INF;
memset(visited,0,sizeof(visited));
for(i=1;i<=n;i++)
{
int ma=-10;
for(j=1;j<=n;j++)
{
if(!visited[j]&&dis[j]>ma)
{
ma=dis[j];
now=j;
}
}
visited[now]=1;
for(j=head[now];j!=-1;j=a[j].next)
{
int x=a[j].ed,y=a[j].val;
if(!visited[x])
dis[x]=max(dis[x],min(dis[now],y));
}
}
}
int main()
{
int cas;
while(~scanf("%d",&cas))
{
int i,j;
for(i=1;i<=cas;i++)
{
scanf("%d",&n);
scanf("%d",&m);
memset(dis,0,sizeof(dis));
memset(head,-1,sizeof(head));
for(j=1;j<=2*m;j++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[j].st=x;a[j].ed=y;a[j].val=z;
a[j].next=head[x];head[x]=j;
a[++j].st=y;a[j].ed=x;a[j].val=z;
a[j].next=head[y];head[y]=j;
}
Dijkstra();
printf("Scenario #%d:\n",i);
printf("%d\n\n",dis[n]);
}
}
return 0;
}
下面是 spfa 代码:
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f using namespace std; struct edge { int st,ed; int val,next; }a[1000000]; int head[1010],dis[1010],visited[1010],m,n; void spfa() { int i; queue
que; que.push(1); dis[1]=INF; memset(visited,0,sizeof(visited)); while(!que.empty()) { int t=que.front(); que.pop(); visited[t]=0; for(i=head[t];~i;i=a[i].next) { int x=a[i].ed,y=a[i].val; if(dis[x]