POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;

Heavy Transportation

Time Limit: 3000MS   Memory Limit: 30000K
        

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个站点(就这样理解吧),m条路,两个站点之间有且只有一条路,每条路都有一个最大载重量,求整个路径组成的图中的最大的承重;比如,1到2之间的载重是3,超过3便不能从这条路上过,而1到3之间的载重是4,这时已经将图连接起来了,所以最大值是4,如果选1->2->3,则1、2之间的路就不能过重量为5的升降机;

这样,就相当于求一个最大生成树的权值最小的那条边,不过用生成树的方法不是超时就RE,只好用最短路中的一个算法解决了,我们看,既然dijkstra是求单源的最短路,d[i]存储的是从起始点到i点的最短路,那么我们就用它来存起始点到i点权值最小的那个;这样答案不就出来了;想想看,这道题逻辑性很强,求能承载的最大重量实际上是求整个联通图中权值最小的,就像短板原理--能装多少水取决于最短的那块木板;

Kruskal生成树:RE,数组开大了又会超时;

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1000+10;
struct node
{
    int u,v,w;
}a[N];
int n,m,f[N];
int find(int x)
{
    return f[x]==-1?x:f[x]=find(f[x]);
}
int cmp(node a,node b)
{
    return a.w>b.w;
}
int ks(int n,int m)
{
    for(int i=0;i<m;i++)
        scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
    sort(a,a+m,cmp);
    int minn=10000000;
    memset(f,-1,sizeof(f));
    for(int i=0;i<m;i++)
    {
        int x=find(a[i].u);
        int y=find(a[i].v);
        if(x!=y)
        {
            minn=min(minn,a[i].w);
            f[x]=y;
        }
        if(find(1)==find(n))
            break;
    }
    return minn;
}
int main()
{
    int t;
    scanf("%d",&t);
    int t1=t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d%d",&n,&m);
        int x=ks(n,m);
        printf("Scenario #%d:\n%d\n",t1-t,x);
    }
    return 0;
}

Dijkstra最短路变形:AC

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1005;
int w[N][N],v[N],d[N],n,m,t,t1;
void dijkstra()
{
    int a,b,c,i,j;
    memset(w,0,sizeof(w));
     for(i=1; i<=m; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        w[a][b]=w[b][a]=c;
    }
     memset(v,0,sizeof(v));
    for(i=1; i<=n; i++)
        d[i]=w[1][i];
    for(i=1;i<=n;i++)
    {
        int x,m=-1;
        for(j=1;j<=n;j++)
            if(!v[j]&&d[j]>m)
            m=d[x=j];
        v[x]=1;
        for(j=1;j<=n;j++)
            if(!v[j] && d[j]<min(d[x],w[x][j]))
             d[j]=min(d[x],w[x][j]);
    }
    printf("Scenario #%d:\n",t1-t);
    printf("%d\n\n",d[n]);
}
int main()
{
    scanf("%d",&t);
    t1=t;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        dijkstra();
    }
    return 0;
}
时间: 2024-10-25 22:11:32

POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;的相关文章

POJ 1797 Heavy Transportation (最短路变形)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20364   Accepted: 5401 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ 3013 Big Christmas Tree【最短路变形,DIjkstra堆优化+spfa算法】

Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23064   Accepted: 4987 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of t

poj1797 Heavy Transportation(最短路变形)

题目大意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量 解题思路:其实这个求最大边可以近似于求最短路,只要修改下找最短路更新的条件就可以了 #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=1010; const int INF=0x3f3f3f3

【图论补完计划】poj 3635 (最短路变形)

Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7427   Accepted: 2399 Description After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you v

Heavy Transportation POJ 1797 最短路变形

Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你求从编号为1的城市到编号为n的城市的路线中,最大能经过多重的车. 解题思路 这个题可以使用最短路的思路,不过转移方程变了\(dis[j]=max(dis[j], min(dis[u], e[u][j]))\).这里dis[j]表示从标号为1的点到达编号为j的点的路径中,最小的承重能力,就像短板效应样

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

POJ 1161 Walls(最短路+枚举)

POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了最外面的一个),现在,如果某几个小镇上的人想要聚会,为选择哪个区域为聚会地点,可以使他们所有人总共需要穿过的墙数最小,题目上有说明,不在某个点上聚会(聚会点在某个多边形内部),行进过程中不穿过图中的点(也就是除出发点外的其他小镇). 输入第1行代表m(2<=M<=200)个区域 第2行代表n(3&

HN0I2000最优乘车 (最短路变形)

HN0I2000最优乘车 (最短路变形) [试题]为了简化城市公共汽车收费系统,某城市决定对大部分的公共汽车都采用一票制,但由于某些公共汽车所经过的停车站太多和路途太长,就采用两票或多票制.经过这种票制改革后,人们坐公共汽车从一个站到另一个站时,就不得不选择一个好的乘车方案,以使他们的乘车费用最低. 为了方便于求出最佳的乘车方案,我们假设: l  采用一票制的公共汽车,无论从哪个站上车到那个站下车,乘该公共汽车的费用为1(费用单位). l  采用多票制的公共汽车,将设立某些站为关键站:那么,如果