1. 程式人生 > >POJ1797 Heavy Transportation

POJ1797 Heavy Transportation

Heavy Transportation
Time Limit: 3000MSMemory Limit: 30000K
Total Submissions: 44962Accepted: 11760

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

題意:找到1~n能運送最大貨物的質量

AC程式碼:prim

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1010;
const int inf=0x3f3f3f3f;
int e[maxn][maxn],dis[maxn];
bool vis[maxn];
int n,m;
void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j) e[i][j]=0;
            else e[i][j]=-inf;
        }
    }
}
int prim(int cur)
{
    memset(vis,false,sizeof(vis));
    vis[cur]=true;
    for(int i=1;i<=n;i++)
    {
        dis[i]=e[cur][i];
    }
    int ans=inf;
    for(int i=1;i<n;i++)
    {
        int maxx=-inf,u;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&maxx<dis[j])
            {
                maxx=dis[j];
                u=j;
            }
        }
        ans=min(ans,maxx);
        if(maxx==-inf||u==n) break;
        vis[u]=true;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]<e[u][j])
            {
                dis[j]=e[u][j];

            }

        }
    }
    return ans;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int t,iCase=1;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        init();
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            e[a][b]=max(e[a][b],c);
            e[b][a]=max(e[b][a],c);
        }
        int ans=prim(1);
        cout<<"Scenario #"<<iCase++<<":"<<endl;
        cout<<ans<<endl<<endl;
    }
    return 0;
}

kruskal:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000010;
struct node
{
    int u,v,d;
}p[maxn];
int pre[maxn];
int n,m;
int Find(int x)
{
    if(x!=pre[x]) pre[x]=Find(pre[x]);
    return pre[x];
}
bool cmp(node a,node b)
{
    return a.d>b.d;
}
int kruskal()
{
    int ans;
    sort(p,p+m,cmp);
    for(int i=0;i<m;i++)
    {
        int tx=Find(p[i].u);
        int ty=Find(p[i].v);
        if(tx!=ty)
        {
            pre[tx]=ty;
            if(Find(1)==Find(n))
            {
                ans=p[i].d;
                break;
            }
        }
    }
    return ans;
}
void init()
{
    for(int i=1;i<=n;i++)
    {
        pre[i]=i;
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    int t,iCase=1;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        init();
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            p[i].u=a;
            p[i].v=b;
            p[i].d=c;
        }
        int ans=kruskal();
        cout<<"Scenario #"<<iCase++<<":"<<endl;
        cout<<ans<<endl<<endl;
    }
    return 0;
}

相關推薦

poj1797 Heavy Transportation(最短路變形)

max while oid amp == %d cos cin std 題目大意:有n個城市,m條道路,在每條道路上有一個承載量,現在要求從1到n城市最大承載量,而最大承載量就是從城市1到城市n所有通路上的最大承載量 解題思路:其實這個求最大邊可以近似於求最短路,只要修改下

POJ1797 Heavy Transportation

Heavy TransportationTime Limit: 3000MSMemory Limit: 30000KTotal Submissions: 44962Accepted: 11760DescriptionBackground Hugo Heavy is happy

POJ1797Heavy Transportation(改造Dijkstra)

i++ 每次 idt ges truct city allow ESS center Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions

POJ-1797 Heavy Transportation(最大生成樹)

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

Heavy Transportation

iostream 定義 OS 中一 str eof math string main POJ - 1797 這題求:1到n的路徑中,邊權最小的定義是min,求出min的最大值) 1 #include<iostream> 2 #include&

POJ 1979 Heavy Transportation (kruskal)

max i++ ret sport eth ecif cit scenario \n           Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissio

POJ 1797 Heavy Transportation

amp posit ont com fort rmi ans cost osi http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown

POJ--1797 Heavy Transportation (最短路)

題目電波: POJ--1797 Heavy Transportation  n點m條邊, 求1到n最短邊最大的路徑的最短邊長度 改進dijikstra,dist[i]陣列儲存源點到i點的最短邊最大的路徑的最短邊長度   #include<iostrea

Heavy Transportation (Dijkstra演算法變形)

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 h

Heavy Transportation(Dijkstra變形)

Heavy Transportation Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a c

POJ-1797 Heavy Transportation(最短路變形)

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

C - Heavy Transportation POJ - 1797[dijkstra]

題意:從1出發到各點的最短距離。 a c  

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

【最多能夠承受的最大重量】poj 1797 Heavy Transportation

Problem Description 輸入T組測試資料,每組測試資料,輸入n,m分別代表n個城市,m條道路接下來m行,每行u,v,w分別代表u,v兩城市之間道路承受的重量。 思路:讓你求出從城市1到城市n最多能夠運輸最大重量,最短路的演算法,改變下

poj 1797 Heavy Transportation 本來以為floyd瞬秒,結果各種re,真無語,看網上別人的並查集了

我就差異這道題我的floyd毫無錯誤,n為1000,我的陣列範圍也夠,怎麼就是re呢,真無語,下面是我的floyd,善良的讀者啊!!求解釋啊!!第二個程式是我看了別人的並查集寫的,但還有地反糾結了in

Heavy Transportation【最大生成樹】

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

POJ 1797 Heavy Transportation(最大生成樹)

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

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

Heavy Transportation(最短路變形)

Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he ne

Poj 1797 Heavy Transportation (最短路變形)

題意:給你一張圖,有n個點,m條邊。讓你求出從1點到n點的所有通路中最小邊的最大值。 題解:最短路的變形。把dijkstra中dis陣列中存的東西改成最小邊的最大值。每次找最大邊來作比較。然後更新di