Heavy Transportation

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 52728   Accepted: 13474

题目链接:http://poj.org/problem?id=1797

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

题意:

问从1号点到n号点所经路径的最小边权的最大值为多少。

题解:

其实本题也不是严格的dijkstra算法,只是利用了类似的贪心思想。

我们首先维护一个到当前点所有路径中的最小值,把它扔进优先队列里面,从优先队列里面每次取大值出来去更新与之相邻的点。

这里的正确性证明和dijkstra算法的证明类似,也就是说一个点去更新其它点后,不会被其他点又一次更新。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int T;
int n,tot,m;
int head[N],vis[N],d[N];
struct Edge{
int u,v,next,w;
}e[N*N<<];
struct node{
int d,u;
bool operator < (const node &A)const{
return d<A.d;
}
};
void adde(int u,int v,int w){
e[tot].v=v;e[tot].next=head[u];e[tot].w=w;head[u]=tot++;
}
void Dijkstra(int s){
priority_queue <node> q;
memset(d,,sizeof(d));memset(vis,,sizeof(vis));
node now;d[s]=INF;
now.d=INF;now.u=s;
q.push(now);
while(!q.empty()){
node cur = q.top();q.pop();
int u=cur.u;
if(vis[u]) continue ;
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]<min(d[u],e[i].w)){
d[v]=min(d[u],e[i].w);
now.d=d[v];now.u=v;
q.push(now);
}
}
}
}
int main(){
cin>>T;
int cnt = ,first=;
while(T--){
cnt++;
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));tot=;
for(int i=;i<=m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
adde(u,v,c);adde(v,u,c);
}
Dijkstra();
printf("Scenario #%d:\n",cnt);
cout<<d[n]<<endl;
cout<<endl;
}
return ;
}

最新文章

  1. ReactNative&amp;weex&amp;DeviceOne对比
  2. 分享一个UI与业务逻辑分层的框架(二)
  3. Oracle如何实现从特定组合中随机读取值
  4. codevs5164 逆波兰表达式
  5. linux shell脚本通过参数名传递参数值
  6. Jquery使select、radio某项选中
  7. 第十四章:样式(Style)和主题(Theme)
  8. DSP中的cmd文件
  9. 如何在winform DataGridView控件的DataGridViewButtonColumn按钮列中禁用按钮
  10. 序列化类型为XX的对象时检测到循环引用
  11. 简单OS(ucos超级精简版)&mdash;&mdash;裸调度器【worldsing笔记】
  12. JDBC_mysql---防sql注入,存储图片
  13. C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法
  14. Think in Java(一):Java基础
  15. 9.Java主要有那几种文件类型?各自的作用是什么?
  16. java多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?
  17. B树和B+树详解
  18. 正则去除html字符串中的注释、标签、属性
  19. Django 基于类的通用视图
  20. Python学习 ——正则表达式

热门文章

  1. mysql5.6主主复制及keepalived 高可用
  2. rhel6.4扩充swap分区
  3. Tensorflow之MNIST的最佳实践思路总结
  4. itchat和matplotlib的结合使用爬取微信信息
  5. abap&lt;itab&gt;refresh,clear,free
  6. &quot;Cannot open source file &quot;Wire.h&quot; &quot; in Arduino Development
  7. Eclipse+APKTool动态调试APK
  8. 算法搬运之BFPRT算法
  9. Django 运行Admin 页面时出现 UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte XXXX解决方法
  10. ubuntu中tensorflow安装