(POJ 1797)Heavy Transportation 最大生成树|最短路变形 (理解最短路核心思想好题)

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 40123 Accepted: 10544
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

题意:
有一个n个节点的无向图,问你从一号节点到n号节点,所有路径中的边的最小值的最大值为多少?

分析:
(1)最大生成树思想:
要想使路径中的最小值的最大值最大,那么我们就尽可能的走权值大的边。所有我们将边从大到小往图中加入,直到1号节点和n号节点联通即可,最后加的边权值即为所求最大值。

(2)最短路核心思想:
我们在求最短路时,d[x]表示节点1到节点x的最短路径的长度。更新时满足:
d[v] > d[u] + w(u,v)
同理,我们设d[x]表示节点1到节点x所有路径中的边的最小值的最大值。满足:
min(d[u],w(u,v)) > d[v] 时更新d[v] = min(d[u],w(u,v))

总结:
已经写过一些关于最短路的spfa算法的题目了,但是我们要理解的不仅仅只是要会求最短路,而是要掌握求最短路的核心思想,这往往是今后真正比赛时解题的关键。感觉就像dp类型的题目一样,大体上的思路是差不多的,只是更新的公式不一样而已。

代码:
Kruskal()

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

const int maxn = 1010;

struct edge
{
    int u,v,w;
}edges[maxn*maxn/2];
int f[maxn];
int n,m,ans;

int cmp(edge aa,edge bb)
{
    if(aa.w > bb.w) return 1;
    else return 0;
}

int findf(int x)
{
    return f[x] == x ? x : f[x] = findf(f[x]);
}

void unionf(int x,int y)
{
    int xx = findf(x);
    int yy = findf(y);
    if(xx != yy)
    {
        f[xx] = yy;
    }
}

int main()
{
    int t,u,v,w,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            edges[i].u = u;
            edges[i].v = v;
            edges[i].w = w;
        }
        sort(edges,edges+m,cmp);
        for(int i=1;i<=n;i++) f[i] = i;
        for(int i=0;i<m;i++)
        {
            unionf(edges[i].u,edges[i].v);
            if(findf(1) == findf(n))
            {
                ans = edges[i].w;
                break;
            }
        }
        printf("Scenario #%d:\n",cas++);
        printf("%d\n",ans);
        if(t) printf("\n");

    }
    return 0;
}

spfa()

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

const int maxn = 1010;
const int INF = 100000000;
struct edge
{
    int v,w,next;
}edges[maxn * maxn / 2];
int n,m,e;
int head[maxn],d[maxn];
bool vis[maxn];

void addedges(int u,int v,int w)
{
    edges[e].v = v;
    edges[e].w = w;
    edges[e].next = head[u];
    head[u] = e++;
    edges[e].v = u;
    edges[e].w = w;
    edges[e].next = head[v];
    head[v] = e++;
}

void spfa(int s)
{
    memset(vis,0,sizeof(vis));
    memset(d,0,sizeof(d));
    queue<int> q;
    d[s] = INF;
    q.push(s);
    vis[s] = 1;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        vis[u] = 0;
        for(int i=head[u];i!=-1;i=edges[i].next)
        {
            int v = edges[i].v;
            int w = edges[i].w;
            if(min(d[u],w) > d[v])
            {
                d[v] = min(d[u],w);
                if(!vis[v])
                {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}

int main()
{
    int u,v,w,t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(head,-1,sizeof(head));
        e = 0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedges(u,v,w);
        }
        spfa(1);
        printf("Scenario #%d:\n",cas++);
        printf("%d\n",d[n]);
        if(t) printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值