首页 > 代码库 > poj-1797 Heavy Transportation

poj-1797 Heavy Transportation

题目链接:http://poj.org/problem?id=1797

求路径中承载量的最小值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

const int MAXV = 4010;
const int inf = 10000000;

int map[MAXV][MAXV];
int d[MAXV];
bool vis[MAXV];
int n,m;

void dijkstra(int s)
{
	for(int i=1;i<=n;i++)
    {
		vis[i]=0;
		d[i]=map[s][i];
	}
	while (1)
    {
		int minn = 0;
		int v = -1;
		for(int i=1;i<=n;i++)
			if(!vis[i] && d[i] > minn)
			{
				v=i;
				minn=d[i];
			}
        if(v == -1)
            break;
		vis[v]=1;
		for(int i=1;i<=n;i++)
			if(!vis[i] && d[i] < min(d[v] , map[v][i]))
				d[i] = min(map[v][i],d[v]);
	}
}

int main()
{
	int i,j,a,b,c,t;
	int cases = 1;
	scanf("%d",&t);
	while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        while (m--)
        {
            scanf("%d %d %d",&a,&b,&c);
                map[a][b] = map[b][a] = c;
        }
		dijkstra(1);
		printf("Scenario #%d:\n",cases++);
		printf("%d\n\n",d[n]);
	}
	return 0;
}


poj-1797 Heavy Transportation