POJ 2263 Heavy Cargo(Floyd + map)

Heavy Cargo

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3768   Accepted: 2013

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

题意:求各城市每条给定起点和终点间通路的最小负载,并据此求出所有最小负载中的最大值.

思路:题目给的是城市名用map.

收获:map用法 + Floyd

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 500

map<string, int> a;
char s1[32];
char s2[32];
int  c[maxn][maxn];
int v[maxn][maxn];
int pre[maxn][maxn];
int main()
{
    int n, m, w;
    int cas = 0;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        int cnt = 0;
        memset(c, 0, sizeof c);
        for(int i = 0; i < m; i++)
        {
            scanf("%s%s%d", s1, s2, &w);
            if(!a.count(s1))
            a[s1] = cnt++;
            if(!a.count(s2))
            a[s2] = cnt++;
            c[a[s1]][a[s2]] = c[a[s2]][a[s1]] = w;
        }
//    map<string, int>::iterator it;
//    for(it=a.begin();it!=a.end();++it)
//        cout<<"key: "<<it->first <<" value: "<<it->second<<endl;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                v[i][j] = c[i][j];
                pre[i][j] = i;
            }

        for(int k = 0; k < n; k++)
            for(int i = 0; i<n; i++)
                for(int j = 0; j < n; j++)
                {
                    v[i][j] = max(v[i][j], min(v[i][k],v[k][j]));
                    pre[i][j] = pre[k][j];
                }

        scanf("%s%s",s1,s2);

        printf("Scenario #%d\n", ++cas);
        printf("%d tons\n",v[a[s1]][a[s2]]);
        puts("");
    }
    return 0;
}
时间: 2024-10-09 04:28:16

POJ 2263 Heavy Cargo(Floyd + map)的相关文章

POJ 2263 Heavy Cargo(ZOJ 1952)

最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF,其他为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来,直到出发和终点能沟通的时候,最小的边就是. Kruskal: #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<ma

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的..但是实在没看出跟优先队列有什么关系..我用的二分+并查集做出来了... 二分路的载重量.然后用并查集检查是否连通. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #

Poj 2263 Heavy Cargo Floyd 求最大容量路

f[i][j] = max(f[i][j],min(f[i][k],f[j][k])) #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cst

poj 1797 Heavy Transportation(最大生成树)

poj 1797 Heavy Transportation 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

poj 2263 Heavy Cargo(floyd+dijkstra)

floyd #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #include<map> #include<iostream> using namespace std; int n,m,edge[250][250],vis[250],dist[250]; map<string,int>a; void floyd() { int

POJ 1797 Heavy Transportation (Dijkstra变形)

F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand busines

POJ 3615 Cow Hurdles (Floyd算法)

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6142   Accepted: 2752 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

POJ 3613 Cow Relays (floyd + 矩阵快速幂)

题目大意: 求刚好经过K条路的最短路 我们知道如果一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B  B[i][j]  就表示   i-j 刚好走过两条路的方法数 那么同理 我们把i-j 的路径长度存到A 中. 在A*A的过程中,不断取小的,那么最后得到的也就是i - j 走过两条路的最短路了. 当然也是利用到了floyd的思想. 然后要求出K次的最短路,那么就是矩阵快速幂的工作了. 注意要离散化.用map #include <cstdio> #include <io

poj 3335 Rotating Scoreboard(半平面交)

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 2550 Description This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the ed