POJ-1797 Heavy Transportation(最大生成树/最短路)

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 44305   Accepted: 11608

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 to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

Source

TUD Programming Contest 2004, Darmstadt, Germany

输入一个图,边的权值表示的是这条路的最大承重量,找到一个从结点1到结点n,承载量最大的路。可以用最短路改一下松弛的部分来做,也可以用最大生成树。

dijkesta:

#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define MOD 1e9+7
using namespace std;
int e[1005][1005];
int dis[1005];
bool vis[1005];
int main()
{
	int T,n,m,ca=0;
	cin>>T;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		memset(e,0,sizeof(e));
		int a,b,w;
		for(int i=0;i<m;i++)
		{
            scanf("%d%d%d",&a,&b,&w);
            e[a][b]=e[b][a]=w;
		}
		dis[1]=INF;
		for(int i=2;i<=n;i++)
            dis[i]=e[1][i];
		memset(vis,false,sizeof(vis));
        for(int k=0;k<n;k++)
        {
            int mi,mm=-INF;
            for(int i=1;i<=n;i++)
            {
                if(!vis[i]&&dis[i]>mm)
                {
                    mm=dis[i];
                    mi=i;
                }
            }
            vis[mi]=true;
            for(int i=1;i<=n;i++)
            {
                if(!vis[i])
                    dis[i]=max(dis[i],min(dis[mi],e[mi][i]));
            }
        }
        printf("Scenario #%d:\n%d\n\n",++ca,dis[n]);
	}
	return 0;
}

kruskal:

#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define MOD 1e9+7
#define MAXN 1005
using namespace std;
int pre[MAXN];

void init (int n)
{
    for(int i=0;i<=n;i++)
        pre[i]=i;
}
struct edge
{
    int x,y,w;
}e[MAXN*MAXN];
bool comp(edge a,edge b)
{
    return a.w>b.w;
}
int find(int x)
{
    if(pre[x]==x)
        return x;
    return pre[x]=find(pre[x]);
}

int main()
{
    int T,ca=0,n,m;
    scanf("%d",&T);
    while(T--)
    {
        int ans=0;
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
        sort(e,e+m,comp);
        for(int i=0;i<m;i++)
        {
            int x=find(e[i].x);
            int y=find(e[i].y);
            if(x!=y)
                pre[x]=y;
            if(find(1)==find(n))
            {
                ans=e[i].w;
                break;
            }
        }
        printf("Scenario #%d:\n%d\n\n",++ca,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39027601/article/details/80157174