UVA 544:
题目介绍:
Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model,
the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the
truck itself. It is only limited by the weight restrictions that apply for the roads along the path you
want to drive.
Given start and destination city, your job is to determine the maximum load of the Godzilla V12
so that there still exists a path between the two specified cities.
题目大意:
给了一系列在两个城市的最大承重,算起点到终点的最大可能载重。
分析:
类似最短路判断。首先对城市名做一个映射,然后直接使用dijastra算法即可。
#Ac代码:
下面展示一些 。
#include<iostream>
#include<map>
#include<cstring>
#include<climits>
using namespace std;
int len,road[205][205];
int main(){
int n,k,Case=1;
while(scanf("%d%d",&n,&k)==2&&n){
len=0;
memset(road,0,sizeof(road));
map<string,int> now;
for(int i=0;i<k;i++){
string a,b;
int c;
cin>>a>>b>>c;
int t1,t2;
if(!now.count(a))now[a]=len++;
if(!now.count(b))now[b]=len++;
t1=now[a];
t2=now[b];
road[t1][t2]=road[t2][t1]=c;
}
string a,b;
int bg,ed;
cin>>a>>b;
bg=now[a];
ed=now[b];
int vis[205]={0},ans[205]={0};
for(int i=0;i<n;i++)
ans[i]=road[bg][i];
vis[bg]=1;
while(1){
int Max=0,k;
for(int i=0;i<n;i++){
if(!vis[i]&&ans[i]>Max){
Max=ans[i];
k=i;
}
}
vis[k]=1;
if(vis[ed])break;
for(int i=0;i<n;i++){
if(!vis[i]&&road[k][i]){
ans[i]=max(ans[i],min(ans[k],road[k][i]));
}
}
}
printf("Scenario #%d\n",Case++);
printf("%d tons\n\n",ans[ed]);
}
return 0;
}