POJ1797 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 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所有路径里最大的最短边权值。可以用堆优化的Dijkstra跑过。不同的是这里d数组的含义以及松弛操作都有所不同。这里d[i]代表从1到i所有路径最小边里最大的边的权值。松弛条件改为if(d[y]<min(d[x],z))d[y]=min(d[x],z).要注意的是:1.d数组要初始化为-INF,因为要求的是d[n]让其尽可能大。2.d[1]要初始化为INF。因为如果按照dij模板初始化d[1]为0,第一次取出的是1号点,这时候d[y]为-INF,必然小于min(d[x],z),因为d[x]在第一次等于d[1]等于0,所以最终d数组将全部为0,得不到答案。2.pair的第一维不用加负号,因为优先队列应该先让大的出来,所以不用按照蓝书上那样让其变为小根堆。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int N=10005,M=200010;//两倍存双向边
int head[N],ver[M],edge[M],Next[M],d[N];
bool v[N];
int n,m,tot=0;
priority_queue<pair<int,int> >q;
 void add(int x,int y,int z)
 {
     ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
 }
 void dijkstra()
 {
     memset(d,-0x3f,sizeof(d));
     memset(v,0,sizeof(v));
     d[1]=2000000000;
     q.push(make_pair(20000000,1));
     while(q.size())
     {
         int x=q.top().second;
         q.pop();
         if(v[x])continue;
         v[x]=1;
         int i;
         for(i=head[x];i;i=Next[i])
         {
             int y=ver[i];
             int z=edge[i];
             if(d[y]<min(d[x],z))
             {
                 d[y]=min(d[x],z);
                 q.push(make_pair(d[y],y));
             }
         }
     }
 }
int main()
{
    int t;
    cin>>t;
    int i,j,k;
    for(i=1;i<=t;i++)
    {
        tot=0;
        while(q.size())q.pop();
        memset(head,0,sizeof(head));
        memset(Next,0,sizeof(Next));
        scanf("%d%d",&n,&m);
        for(j=1;j<=m;j++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        dijkstra();
        printf("Scenario #%d:\n",i);
        cout<<d[n]<<endl;
        cout<<endl;
    }
}

原文地址:https://www.cnblogs.com/lipoicyclic/p/12319665.html

时间: 2024-08-14 10:46:22

POJ1797 Heavy Transportation (堆优化的Dijkstra变形)的相关文章

poj1797 - Heavy Transportation(最大边,最短路变形spfa)

题目大意: 给你以T, 代表T组测试数据,一个n代表有n个点, 一个m代表有m条边, 每条边有三个参数,a,b,c表示从a到b的这条路上最大的承受重量是c, 让你找出一条线路,要求这条线路上的最大的承重, 在所有其他线路最小. 题目分析: 这里只要将spfa进行一下变形就可以解决这问题了. 首先 我们的dist数组,起点位置要初始化为 INF, 其他位置初始化为 0 然后我们更新 dist 数组, 结果输出 dist[n]就行了 为什么这样写: 因为我们每次要找 所有路径中的最大边的最小一个,

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的点的路径中,最小的承重能力,就像短板效应样

Heap+Dijkstra堆优化的Dijkstra

前面说到"原生的Dijkstra",由于Dijkstra采用的是贪心策略,在贪心寻找当前距离源结点最短的结点时需要遍历所有的结点,这必然会导致效率的下降,时间复杂度为n^n.因此当数据量较大时会消耗较长时间.为了提高Dijkstra的效率,只有对Dijkstra的贪心策略进行改进. 由于Dijkstra采用的贪心策略是每次寻找最短距离的结点并将其放入存放所有已知最短距离结点的S集合中,可以联想到堆以及优先级队列这些数据结构,这些结构都能非常高效地提供当前状态距离最短的结点.实践也可以证

堆优化的Dijkstra

SPFA在求最短路时不是万能的.在稠密图时用堆优化的dijkstra更加高效: 1 typedef pair<int,int> pii; 2 priority_queue<pii, vector<pii>, greater<pii> > q 3 void dijkstra(){ 4 memset(dis,10,sizeof(dis)); 5 memset(vis,0,sizeof(vis)); 6 dis[K]=0; 7 q.push(make_pair(d

POJ1797 Heavy Transportation 【Dijkstra】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted: 5569 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 1797 Heavy Transportation 【最短路Dijkstra 变式】

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

HDU4725 The Shortest Path in Nya Graph(堆优化的dijkstra算法)

题意: 这是一个非常容易解决的问题,您的任务只是计算图像,而仅是计算干草成本和算法成本.如果您不懂此段话,请继续.Nya图是具有“层”的无向图.图中的每个节点都属于一个层,总共有N个节点.您可以以成本C从x层中的任何节点移动到x + 1层中的任何节点,因为道路是双向的,因此也可以以相同的成本从x + 1层移动到x层.此外,还有M个额外的边,每个边连接一对节点u和v,成本为w.帮助我们计算从节点1到节点N的最短路径. 题解: 主要是建图. N个点,然后有N层,要假如2*N个点. 总共是3*N个点.

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

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

poj1797 Heavy Transportation (Kruskal 算法)

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