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=,M=;//两倍存双向边
int head[N],ver[M],edge[M],Next[M],d[N];
bool v[N];
int n,m,tot=;
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,,sizeof(v));
d[]=;
q.push(make_pair(,));
while(q.size())
{
int x=q.top().second;
q.pop();
if(v[x])continue;
v[x]=;
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=;i<=t;i++)
{
tot=;
while(q.size())q.pop();
memset(head,,sizeof(head));
memset(Next,,sizeof(Next));
scanf("%d%d",&n,&m);
for(j=;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;
}
}

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

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

    题目大意: 给你以T, 代表T组测试数据,一个n代表有n个点, 一个m代表有m条边, 每条边有三个参数,a,b,c表示从a到b的这条路上最大的承受重量是c, 让你找出一条线路,要求出在这条线路上的最小 ...

  2. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  3. 堆优化的Dijkstra

    SPFA在求最短路时不是万能的.在稠密图时用堆优化的dijkstra更加高效: typedef pair<int,int> pii; priority_queue<pii, vect ...

  4. POJ--1797 Heavy Transportation (最短路)

    题目电波: POJ--1797 Heavy Transportation n点m条边, 求1到n最短边最大的路径的最短边长度 改进dijikstra,dist[i]数组保存源点到i点的最短边最大的路径 ...

  5. 朴素版和堆优化版dijkstra和朴素版prim算法比较

    1.dijkstra 时间复杂度:O(n^2) n次迭代,每次找到距离集合S最短的点 每次迭代要用找到的点t来更新其他点到S的最短距离. #include<iostream> #inclu ...

  6. POJ1797 Heavy Transportation —— 最短路变形

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  7. POJ1797 Heavy Transportation 【Dijkstra】

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

  8. (Dijkstra) POJ1797 Heavy Transportation

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

  9. 学习笔记·堆优化$\mathscr{dijkstra}$

    嘤嘤嘤今天被迫学了这个算法--其实对于学习图论来说我内心是拒绝的\(\mathscr{qnq}\) 由于发现关于这个\(\mathscr{SPFA}\)的时间复杂度\(O(kE)\)中的\(k \ap ...

随机推荐

  1. Strategic game树形DP解法(Poj1463,Uva1292)

    已经写过本题用二分图的做法,见这儿. 本题的图是一棵树,求最小点覆盖也可以用树形DP的做法. 定义状态f[0/1][u]表示以u为根的子树,u选取/不选最少需要选取多少点来覆盖. 显然 f[0][u] ...

  2. emoji大全_如何明智地使用emoji: &#128516;

    官网 https://www.webfx.com/tools/emoji-cheat-sheet/ emoji那么多,如何准确地使用到自己想要到呢?

  3. Go_MySQL查询插入删除

    什么是预处理? 普通SQL语句执行过程: 客户端对SQL语句进行占位符替换得到完整的SQL语句. 客户端发送完整SQL语句到MySQL服务端 MySQL服务端执行完整的SQL语句并将结果返回给客户端. ...

  4. STA之RC Corner再论

    Q:RC-Corner跟PVT怎么组合? A:通常的组合:   Q:通常说的ttcorner指的是啥? A:@孟时光 ttcorner是指管子在tt+RCtyp吧. Typesof corners W ...

  5. alibaba-java-style-guide

    (一) 命名规约 1.[强制]代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ / name$ / O ...

  6. linux nmon安装

    系统版本红帽7.7: [root@hostuser1 nmon_permon]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core ...

  7. POJ 2018 Best Cow Fences(二分答案)

    题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...

  8. Tensorflow版本更改所产生的问题及解决方案

    1.module 'tensorflow' has no attribute 'mul' tf.mul已经在新版本中被移除,使用 tf.multiply 代替 解决方法 将tf.mul(input1, ...

  9. 【visio】跨职能流程图

    归属于 流程图类别 相比于普通流程图,突出了参与流程的组织.部门之间的联系,形式化地说,它突出的是参与流程的对象之间的联系. 它除了表达基本流程,同时也能展示每个每个流程的归属方,让每个对象明确知道自 ...

  10. VMware 搭建linux虚拟机环境

    1.任务管理器-服务 确认VMware服务是否启动 2.VMware生成网关地址 编辑--虚拟网络编辑器 VMnet8 NAT设置子网IP,子网掩码,网关 3.windows网络--更改适配器设置-- ...