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 i,j,k,t;
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                if(edge[i][k]==-1||edge[k][j]==-1) continue;
                t=min(edge[i][k],edge[k][j]);
                if(edge[i][j]==-1) edge[i][j]=t;
                else if(edge[i][j]<t) edge[i][j]=t;
            }
}

int main()
{
    int i,j,tot,st,ed,cas=0,u,v,w;
    string s1,s2;
    while(~scanf("%d%d",&n,&m))
    {
        memset(edge,0,sizeof(edge));
        if(n==0&&m==0) break;
        a.clear();
        tot=1;
        for(i=0; i<m; i++)
        {
            cin>>s1>>s2>>w;
            if(a[s1]==0)
            {
                a[s1]=tot;
                tot++;
            }
            if(a[s2]==0)
            {
                a[s2]=tot;
                tot++;
            }
            u=a[s1];
            v=a[s2];
            edge[u][v]=w;
            edge[v][u]=w;
        }
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                if(edge[i][j]==0) edge[i][j]=-1;
            }
        cin>>s1>>s2;
        st=a[s1];
        ed=a[s2];
        floyd();
        printf("Scenario #%d\n",++cas);
        printf("%d tons\n\n",edge[st][ed]);
    }
    return 0;
}

dijkstra

cpp

时间: 2024-07-28 17:03:20

poj 2263 Heavy Cargo(floyd+dijkstra)的相关文章

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 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 + 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

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

poj2263 Heavy Cargo --- floyd求最大容量路

求给定起点到终点的路径中,最小边权的最大值 #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f

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 1797 Heavy Transportation【Dijkstra最短路变形】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 29682   Accepted: 7919 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

(简单) POJ 1797 Heavy Transportation,Dijkstra。

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 build his giant steel crane t

poj2263 zoj1952 Heavy Cargo(floyd||spfa)

这道题数据范围小,方法比较多.我用floyd和spfa分别写了一下,spfa明显有时间优势. 一个小技巧在于:把城市名称对应到数字序号,处理是用数字. 方法一:spfa #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include&