POJ 1797 Heavy Transportation 【最大生成树的最小边/最小瓶颈树】

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

 

 

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 50000+20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dp[1005][1005],dis[maxn];
int t,n,m,u,v,w;
int x[maxn],y[maxn],vis[maxn];
int ca;
void dij()
{
    ms(vis,0);
    rep(i,1,n) dis[i]=dp[1][i];
    dis[1]=0;
    vis[1]=1;
    for(int i=1;i<n;i++) //
    {
        int Max=0,k=0;
        rep(j,1,n)
            if(!vis[j] && dis[j]>Max)
                Max=dis[k=j];
        vis[k]=1;
        rep(j,1,n)
            if(!vis[j] && dis[j]<min(dis[k], dp[k][j]))
                dis[j]=min(dis[k],dp[k][j]);
    }
}
int main()
{
    ca=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        ms(vis,0);
        ms(dp,0);
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            dp[u][v]=dp[v][u]=w;
        }
        dij();
        printf("Scenario #%d:\n%d\n\n",ca++,dis[n]);
    }
}
dij

 

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5+20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int fa[maxn],dis[maxn];
int t,n,m,u,v,w;
int vis[maxn];
int ca;
struct node
{
    int u,v,w;
    bool operator < (const node &x) const{
        return w>x.w;
    }
}e[maxn<<1];
int Find(int x)
{
    return x==fa[x]?x:Find(fa[x]);
}

int main()
{
    ca=1;
    scanf("%d",&t);
    while(t--)
    {
        int ans=INF;
        scanf("%d%d",&n,&m);
        rep(i,1,n) fa[i]=i;
        rep(i,1,m)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        }
        sort(e+1,e+m+1);
        rep(i,1,m)
        {
            int fx=Find(e[i].u);
            int fy=Find(e[i].v);
            if(Find(1)!=Find(n)) //源汇点不在同一连通分量就一直加边
            {
                ans=e[i].w;
                fa[fx]=fy;
            }
            else break; //起点和终点一旦连通那么解就是这条边了
        }
        printf("Scenario #%d:\n%d\n\n",ca++,ans);
    }
}
kruskal

 

posted @ 2018-08-08 15:11  Roni_i  阅读(205)  评论(0编辑  收藏  举报