一道图论题目pku2263“Heavy Cargo”我还是用floyid算法
2006-07-24 22:27
375 查看
虽然floyid算法效率不高,三重循环,但是比较容易理解,通过适当的剪支还是可以 提高效率的。但是比赛时间有限,而且时间给的也够,因此我的算法的时间复杂度是O(n*n*n)。
我的代码:
/*
Heavy Cargo
Time Limit:1000MS Memory Limit:65536K
Total Submit:225 Accepted:140
Description
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.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.
Output
For each test case, print three lines:
a line saying "Scenario #x" where x is the number of the test case
a line saying "y tons" where y is the maximum possible load
a blank line
Sample Input
4 3
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Muenchen
5 5
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Hamburg 220
Hamburg Muenchen 170
Muenchen Karlsruhe
0 0
Sample Output
Scenario #1
80 tons
Scenario #2
170 tons
Source
Ulm Local 1998
*/
#include "iostream"
#include "memory"
using namespace std;
int main()
{
int n,r,w,dis[200][200],t=1,flag=0,i,j,k,z=0,min,max;
char ch[30];
char p[200][30];
while(cin>>n>>r&&n>=2&&n<=200&&r>=1&&r<=19900)
{
memset(p,'a',sizeof(p));
memset(dis,0,sizeof(dis));
for(;r-1>=0;r--)
{
cin>>ch;
if(p[0][0]=='a')
{
strcpy(p[0],ch);
z++;
}
for(i=0;i<z;i++)
{
if(!strcmp(p[i],ch))
{
flag=1;
break;
}
}
if(!flag)
{
i++;
strcpy(p[z],ch);
z++;
}
flag=0;
cin>>ch;
for(j=0;j<z;j++)
{
if(!strcmp(p[j],ch))
{
flag=1;
break;
}
}
if(!flag)
{
strcpy(p[z],ch);
z++;
}
flag=0;
cin>>w;
dis[i][j]=w;
dis[j][i]=dis[i][j];
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(dis[i][k]>dis[k][j])
min=dis[k][j];
else
min=dis[i][k];
if(dis[i][j]>min)
max=dis[i][j];
else
max=min;
dis[i][j]=max;
}
cin>>ch;
for(i=0;i<z;i++)
{
if(!strcmp(p[i],ch))
{
break;
}
}
cin>>ch;
for(j=0;j<z;j++)
{
if(!strcmp(p[j],ch))
{
break;
}
}
cout<<"Scenario #"<<t<<endl;
cout<<dis[i][j]<<" tons"<<endl;
t++;
z=0;
}
return 0;
}
我的代码:
/*
Heavy Cargo
Time Limit:1000MS Memory Limit:65536K
Total Submit:225 Accepted:140
Description
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.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.
Output
For each test case, print three lines:
a line saying "Scenario #x" where x is the number of the test case
a line saying "y tons" where y is the maximum possible load
a blank line
Sample Input
4 3
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Muenchen
5 5
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Hamburg 220
Hamburg Muenchen 170
Muenchen Karlsruhe
0 0
Sample Output
Scenario #1
80 tons
Scenario #2
170 tons
Source
Ulm Local 1998
*/
#include "iostream"
#include "memory"
using namespace std;
int main()
{
int n,r,w,dis[200][200],t=1,flag=0,i,j,k,z=0,min,max;
char ch[30];
char p[200][30];
while(cin>>n>>r&&n>=2&&n<=200&&r>=1&&r<=19900)
{
memset(p,'a',sizeof(p));
memset(dis,0,sizeof(dis));
for(;r-1>=0;r--)
{
cin>>ch;
if(p[0][0]=='a')
{
strcpy(p[0],ch);
z++;
}
for(i=0;i<z;i++)
{
if(!strcmp(p[i],ch))
{
flag=1;
break;
}
}
if(!flag)
{
i++;
strcpy(p[z],ch);
z++;
}
flag=0;
cin>>ch;
for(j=0;j<z;j++)
{
if(!strcmp(p[j],ch))
{
flag=1;
break;
}
}
if(!flag)
{
strcpy(p[z],ch);
z++;
}
flag=0;
cin>>w;
dis[i][j]=w;
dis[j][i]=dis[i][j];
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(dis[i][k]>dis[k][j])
min=dis[k][j];
else
min=dis[i][k];
if(dis[i][j]>min)
max=dis[i][j];
else
max=min;
dis[i][j]=max;
}
cin>>ch;
for(i=0;i<z;i++)
{
if(!strcmp(p[i],ch))
{
break;
}
}
cin>>ch;
for(j=0;j<z;j++)
{
if(!strcmp(p[j],ch))
{
break;
}
}
cout<<"Scenario #"<<t<<endl;
cout<<dis[i][j]<<" tons"<<endl;
t++;
z=0;
}
return 0;
}
相关文章推荐
- 一道有关图论的题目
- VKCUP 2012 B Taxi 个人认为一道比较好的题目(虽然已经被否决),还是来发下代码吧
- 还是一道旋转数组的题目
- 一道图论小题目
- CodeForces 875B ( Sorting the Coins) 难度蛮大的一道题目 题目说了一堆主要还是找规律
- 一道关于时间复杂度的数据结构题目
- 还是畅通工程(2006浙江大学研究生上机复试题目[最小生成树] hdoj 1233)
- 一道c++的算法题目
- 一道C语言的内存管理题目
- 关于Java的一道内存的题目
- 一道Java题目
- 【转载】图论题目总结
- 一道算法题目的解法
- 一道关于笔试的多线程题目
- HDOJ 题目1233 还是畅通工程(最小生成树,模板)
- 关于一道软设题目
- 一道小学题目
- 题目1015:还是A+B(%运算符)
- 一道概率题目
- 一道C语言安全编码题目