POJ 2263 Heavy Cargo(Dijkstra算法)
来源:互联网 发布:linux内核熵池 编辑:程序博客网 时间:2024/04/30 17:02
Description
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
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
- 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 3Karlsruhe Stuttgart 100Stuttgart Ulm 80Ulm Muenchen 120Karlsruhe Muenchen5 5Karlsruhe Stuttgart 100Stuttgart Ulm 80Ulm Muenchen 120Karlsruhe Hamburg 220Hamburg Muenchen 170Muenchen Karlsruhe0 0
Sample Output
Scenario #180 tons Scenario #2170 tons
Source
//Dijkstra算法
//每次要找出最大装载的车道,然后连向其他的city[j],这个最大装载比到city【j】后面的装载比行比较,得出小的装载,再和原来可以直接到达的city[j]的
//装载进行比较,得出较大就是city[j]的最大最终装载
#pragma warning(disable:4768)
#include<iostream>
#include<map>
#include<string>
using namespace std;
#define N 202
int g[N][N], dist[N];
bool visited[N];
map<string, int>m;
int min(int a, int b) {
return a<b? a:b;
}
int max(int a, int b) {
return a>b? a:b;
}
void dijkstra(int v, int n)
{
int i, j;
for(i = 0; i <= n; i++) {
dist[i] = g[v][i];
visited[i] = false;
}
visited[v] = true; dist[v] = 0;
for(i = 1; i < n; i++) {
int tmp = 0;
int k = v;
for(j = 1; j <= n; j++) {
if((!visited[j]) && (tmp < dist[j])) {
tmp = dist[j];
k = j;
}
}
visited[k] = true;
for(j = 1; j <= n; j++) {
if((!visited[j]) && (g[k][j] > -1)) {
dist[j] = max(min(dist[k], g[k][j]), dist[j]);
}
}
}
}
int main()
{
int i, j, n, r, x, cnt, C= 0;
char s1[33], s2[33];
while(true) {
cin>>n>>r;
if(n == 0 && r == 0) break;
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
g[i][j] = -1;
m.clear();
for(i = 0, cnt = 1; i < r; i++) {
cin>>s1>>s2>>x;
if(!m[s1]) m[s1] = (cnt++);
if(!m[s2]) m[s2] = (cnt++);
g[m[s1]][m[s2]] = x;
g[m[s2]][m[s1]] = x;
}
cin>>s1>>s2;
dijkstra(m[s1], n);
printf("Scenario #%d\n", ++C);
printf("%d tons\n", dist[m[s2]]);
printf("\n");
}
return 0;
}
- POJ 2263 Heavy Cargo(Dijkstra算法)
- POJ 2263 Heavy Cargo 最短路dijkstra
- POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- POJ:2263 Heavy Cargo
- POJ-2263 Heavy Cargo
- POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo
- POJ 2263 Heavy Cargo (SPFA+Dijkstra,最短路变形)
- ZOJ1592 POJ2263 Heavy Cargo,Dijkstra算法
- [floyd]POJ 2263 Heavy Cargo
- poj 2263 Heavy Cargo(floyd)
- poj_2263 Heavy Cargo (Dijkstra)
- (Relax dijkstra1.2)POJ 2263 Heavy Cargo(使用dijkstra来求解最大生成树问题)
- POJ 2263 Heavy Cargo(图的遍历)
- 玩微博的朋友们请关注我一下,@爪哇米工作室,新浪和腾讯的都可以!
- 排序算法总结
- Objective-C MD5加密字符串
- How to create a Maven web app and deploy to Tomcat - fast
- usb otg的一些简介
- POJ 2263 Heavy Cargo(Dijkstra算法)
- iphone
- 字符编码
- android 相对布局
- NSString的使用
- ASP.NET 企业架构
- malloc()与 calloc()区别 (转)
- 串的模式匹配 经典KMP算法
- 用GDB调试程序(一)