1. 程式人生 > >Heavy Transportation【最大生成樹】

Heavy Transportation【最大生成樹】

Heavy Transportation
Time Limit: 3000MSMemory Limit: 30000K
Total Submissions: 43148Accepted: 11340

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


思路:

題目要求1~n的路徑中最小權值儘量大。我們可以用最大生成樹。先對邊的權值進行從大到小sort排序。然後合併,知道1和n合併到一起,此時的邊的權值就是要求的路徑的最大承載。

程式碼:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 1<<29
int n,m,pre[1005];
int G[1005][1005];
int book[1005][1005];
struct edge
{
    int u,v,w;
}e[1000005];
bool cmp(edge a,edge b)
{
    return a.w > b.w;
}
int find(int u)
{
    int r=u;
    while(r!=pre[r])
        r=pre[r];
    int tmp;
    while(pre[u]!=r)
    {
        tmp=pre[u];
        pre[u]=r;
        u=tmp;
    }
    return r;
}
int merge(int u,int v)
{
    int f1=find(u);
    int f2=find(v);
    if(f1!=f2)
    {
        pre[f2]=f1;
        return 1;
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int tt=1;tt<=t;tt++)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            pre[i]=i;
        memset(G,0,sizeof G);
        memset(book,0,sizeof book);
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            book[u][v]=book[v][u]=max(book[u][v],w);
        }
        m=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                e[m].u=i;
                e[m].v=j;
                e[m++].w=book[i][j];
            }
        }
        sort(e,e+m,cmp);
        int ans=inf;
        for(int i=0;i<m;i++)
        {
            merge(e[i].u,e[i].v);
            if(find(1)==find(n))
            {
                ans=e[i].w;
                break;
            }
        }
        if(tt>1) printf("\n");
        printf("Scenario #%d:\n",tt);
        printf("%d\n",ans);
    }
    return 0;
}


相關推薦

Heavy Transportation成樹

Heavy TransportationTime Limit: 3000MSMemory Limit: 30000KTotal Submissions: 43148Accepted: 11340DescriptionBackground Hugo Heavy is happy

UOJ274 [清華集訓2016] 溫暖會指引我們前行 LCT成樹

生成 size from down print truct ack pty play 題目分析: 差評,最大生成樹裸題。hack數據還卡常。 代碼: 1 #include<bits/stdc++.h> 2 using namespace std;

Pseudoforest HDU - 3367成樹Kruskal

題目連結   題意:好坑的題啊,我一開始看了測試樣例以為是放進一棵樹中問最大的邊,每棵樹上最多成一個環,沒想可以放進多棵樹中,但是要求的是最大的邊權總和就行,那麼就是Kruskal的思想了,以降序排序,然後逐步存入邊,遇到已經成環的就放棄掉這條邊,因為它一定不是最優解。 #

POJ 1797 Heavy Transportation成樹

題目大意:給定n個頂點,以及m條邊的描述,每條邊的描述包括:起點、終點、權重。現在要從頂點1出發到達頂點n,求路徑中能夠承受的最大權重。 解題思路:讀懂題意很重要,樣例比較水,要去深入理解題目,同時注意輸出格式。 1)本題要求出的是從頂點1到頂點n的所有可行路徑中各邊權值

poj 3723 Conscription 成樹|大權森林

題意:要徵兵n個男兵和m個女兵,每個花費10000元,但是如果已經徵募的男士兵中有和將要徵募的女士兵關係好的,那麼可以減少花費,給出關係,求最小花費。 分析:這個題目初始一個是個二分圖,以為可以從這裡入手,但是這個題目這個性質沒用。 初始花費沒人10000,那麼減去其

POJ-1797 Heavy Transportation(成樹)

ngs accept source total project tween task father then Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submiss

NOIP2013貨車運輸 成樹+倍增

nbsp get 最小值 每次 pac def tin map math 題目大意:給你一張n個點m條邊的圖,有q次詢問,每次讓你找出一條從x至y的路徑,使得路徑上經過的邊的最小值最大,輸出這個最大的最小值。 顯然,經過的路徑必然在這張圖的最大生成樹上。 我們求出這個圖

「NOIP2013」「LuoguP1967」貨車運輸(成樹 倍增 LCA 「LuoguP4180」 模板嚴格次小生成樹[BJWC2010](倍增 LCA Kruscal

題目描述 AA國有nn座城市,編號從 11到nn,城市之間有 mm 條雙向道路。每一條道路對車輛都有重量限制,簡稱限重。現在有 qq 輛貨車在運輸貨物, 司機們想知道每輛車在不超過車輛限重的情況下,最多能運多重的貨物。 輸入輸出格式 輸入格式: &nb

hdu——3367 並查集+成樹模板

In graph theory, a pseudoforest is an undirected graph in which every connected component has at most one cycle. The maximal pseudofores

NOIP 2013 Day1 T3貨車運輸(成樹+LCA)

題目描述 Description A 國有 n 座城市,編號從 1 到 n,城市之間有 m 條雙向道路。每一條道路對車輛都有重量限制,簡稱限重。現在有 q 輛貨車在運輸貨物,司機們想知道每輛車在不超過車輛限重的情況下,最多能運多重的貨物。

POJ 1797 Heavy Transportation 短路思維+承載

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 stree

poj 1797Heavy Transportation(成樹

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K

貨車運輸(成樹+LCA)

整數 fine std 一個 ext getchar() 最小路徑 ont getch 題目描述 A 國有 n 座城市,編號從 1 到 n,城市之間有 m 條雙向道路。每一條道路對車輛都有重量限制,簡稱限重。現在有 q 輛貨車在運輸貨物, 司機們想知道每輛車在不超過車輛限重

POJ 1459 &amp;&amp; ZOJ 1734--Power Network流dinic

問題 -m memory ret wid bsp man 最大 中轉 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions:

BZOJ 3943 [Usaco2015 Feb]SuperBull:成樹

sort -1 iostream php ref log ring nbsp 題目 題目鏈接:http://www.lydsy.com/JudgeOnline/problem.php?id=3943 題意:   有n只隊伍,每個隊伍有一個編號a[i]。   每場比賽有兩支隊

luogu1967[NOIP2013D1T3] 貨車運輸 (成樹+LCA)

etc 註意 bre 限制 文件名 con style ont 描述 題目描述 A 國有 n 座城市,編號從 1 到 n,城市之間有 m 條雙向道路。每一條道路對車輛都有重量限制,簡稱限重。現在有 q 輛貨車在運輸貨物, 司機們想知道每輛車在不超過車輛限重的情況下,最多能運

成樹 - 堆優化

printf edge ans can set {} con rim continue const int inf = 1<<29; int n, m; int edge[1005][1005]; int d[1005]; bool vis[1005]; s

zoj 3471 Most Powerful (有向圖)成樹 狀壓dp

最大值 href != 氣體 state span 生成 long logs 題目鏈接 題意 \(N\)種氣體,\(i\)氣體與\(j\)氣體碰撞會: 產生\(a[i][j]\)的威力; 導致\(j\)氣體消失。 求產生威力之和的最大值。 思路 和前幾題找圖上路徑的題不

1305. [CQOI2009]跳舞流+二分

else edge fine string 男女 nbsp CP ios clu Description 一次舞會有n個男孩和n個女孩。每首曲子開始時,所有男孩和女孩恰好配成n對跳交誼舞。每個男孩都不會和同一個女孩跳兩首(或更多)舞曲。有一些男孩女孩相互喜歡,而其他相互不

POJ 1273 Drainage Ditches流模版

ems 操作 AC LG 尋找 In size 兩種 ret 題意:現在有m個池塘(從1到m開始編號,1為源點,m為匯點),及n條有向水渠,給出這n條水渠所連接的點和所能流過的最大流量,求從源點到匯點能流過的最大流量 Dinic 1 #include<ios