Floyd算法解决POJ 2263
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.
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.
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
这道题目的关键就在于松驰操作的改变了。

#include "iostream"
using namespace std;
#define size 201
int map[size][size];
char name[size][500];
int n, begin, end, sum;
int Min(int a, int b)
{
return a>b?b:a;
}
int Str_Int(char name1[])
{
int i;
for(i=0; i<sum; i++)
if(strcmp(name1, name[i])==0)
return i+1;
strcpy(name[sum++], name1);
return sum;
}
void floyd()
{
for(int m=1; m<=n; m++)
{
for(int s=1; s<=n; s++)
{
if(map[s][m])
{
for(int e=1; e<=n; e++)
{
if(map[m][e])
{
if(map[s][e] < Min(map[s][m], map[m][e]))
map[s][e] = Min(map[s][m], map[m][e]);
}
}
}
}
}
}
int main()
{
int r, i, node1, node2, val;
char name1[200], name2[200];
int k=0;
while(cin>>n>>r && (n+r))
{
k++;
sum = 0;
memset(name, 0, sizeof(name));
memset(map, 0, sizeof(map));
for(i=0; i<r; i++)
{
cin>>name1>>name2>>val;
node1 = Str_Int(name1);
node2 = Str_Int(name2);
map[node1][node2]=map[node2][node1]=val;
}
floyd();
cin>>name1>>name2;
begin = Str_Int(name1);
end = Str_Int(name2);
cout<<"Scenario #"<<k<<endl<<map[begin][end]<<" tons"<<endl<<endl;
}
return 0;
}
posted on 2011-10-04 11:52 More study needed. 阅读(383) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架