码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1797 Heavy Transportation(二分+并查集/kruskal)

时间:2015-08-16 15:16:01      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:acm   算法   二分   并查集   kruskal   

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 24398   Accepted: 6472

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

题目大意:

有n个城市,m条路,从1走到n的道路的最大承载量,最大承载量就是走过的路的承载量的最小值。呵呵呵~~~再次举个栗子:假设n=3,m=3,连接1与2的城市的路的承载量为3,连接1与3的承载量是4,连接2与3的承载量是5,那么从1到3有2种走法:(1)1-2-3,承载量依次是3,5,那么最大承载量就是5;(2)1-3,承载量是4,最大承载量也是4,所以答案是4。

解题思路:
最大化最小值,明显的二分。此外,这道题还可以用kruskal变形来做,求使得1与n连通的一棵最大生成树。具体做法就是把边按照权值降序排列,按照权值从大到小的顺序依次将边添加到生成树上,当1于n连通时,输出答案。

参考代码:

/*
二分+并查集
Memory: 1288 KB   	Time: 250 MS  
Language: G++   	Result:  Accepted  
*/
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=1100;

int n,m,par[MAXN],s[MAXN*MAXN],e[MAXN*MAXN],cost[MAXN*MAXN];

int find(int x)
{
    return x==par[x]?x:par[x]=find(par[x]);
}

void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y)
        return;
    par[x]=y;
}

bool judge(int x)
{
    for(int i=1;i<=n;i++)
        par[i]=i;
    for(int i=1;i<=m;i++)
        if(cost[i]>=x)
            unite(s[i],e[i]);
    return find(1)==find(n);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int tcase,f=0;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&s[i],&e[i],&cost[i]);
        int l=1,r=1e6+50;
        while(l<r)
        {
            int mid=(l+r)/2;
            if(judge(mid))
                l=mid+1;
            else
                r=mid;
        }
        printf("Scenario #%d:\n%d\n\n",++f,r-1);
    }
    return 0;
}
/*
最大生成树
Memory: 14904 KB   	Time: 454 MS  
Language: G++   	Result:  Accepted  
*/
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=1100;
struct edge
{
    int s,e,v;
    bool operator<(const edge& t)const
    {
        return v>t.v;
    }
};

edge e[MAXN*MAXN];
int n,m,par[MAXN],ans;

int find(int x)
{
    return x==par[x]?x:par[x]=find(par[x]);
}

bool unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y)
        return false;
    par[x]=y;
    return true;
}

bool same(int x,int y)
{
    return find(x)==find(y);
}

void kruskal()
{
    sort(e+1,e+1+m);
    for(int i=1; i<=m; i++)
    {
        if(!same(e[i].s,e[i].e))
        {
            unite(e[i].s,e[i].e);
            ans=min(ans,e[i].v);
            if(same(1,n))
                break;
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int tcase,f=0;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            par[i]=i;
        memset(e,0,sizeof(e));
        for(int i=1; i<=m; i++)
            scanf("%d%d%d",&e[i].s,&e[i].e,&e[i].v);
        ans=INF;
        kruskal();
        printf("Scenario #%d:\n%d\n\n",++f,ans);
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1797 Heavy Transportation(二分+并查集/kruskal)

标签:acm   算法   二分   并查集   kruskal   

原文地址:http://blog.csdn.net/noooooorth/article/details/47702039

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!