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;
}
}

最新文章

  1. JS通过ajax动态读取xml文件内容
  2. Eclipse中WEB项目自动部署到Tomcat
  3. 读取xml格式文件
  4. 【网络流24题】No.4 魔术球问题 (二分+最小路径覆盖)
  5. intent-filter data Uri 意图过滤器 详解
  6. Android Studio does not point to a valid jvm
  7. 字串变换 (2002 年NOIP全国联赛提高组)
  8. 随心测试_软测基础_001&lt;说在开始_测试理念&gt;
  9. C&amp;C++ Calling Convention
  10. 洛谷P3343 [ZJOI2015]地震后的幻想乡 [DP,概率期望]
  11. DB2数据库常用的函数
  12. jquery.validator 手机号验证
  13. Pytorch_01 Tensor,Autograd,构建网络
  14. vue 中 使用百度编辑器 UEditor
  15. SQL Server 之 子查询与嵌套查询
  16. Removing Timezone from XMLGregorianCalendar
  17. JQuery UI之Autocomplete(2)后端获取数据
  18. Guava之Iterables使用示例
  19. RN中关于ListView的使用
  20. HADOOP百度云资料

热门文章

  1. 【14】N的二进制中1的个数
  2. Chrome浏览器支持跨域访问
  3. 【New】WoSo_我搜 正式上线
  4. Java代码如何关联Hadoop源码
  5. lamp与zabbix开机自启
  6. 小白科普:Netty有什么用?
  7. Mysql数据库内置功能之函数
  8. 2017年陕西省网络空间安全技术大赛——人民的名义-抓捕赵德汉2——Writeup
  9. Python学习之列表篇
  10. linux中的diff命令