Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 22440   Accepted: 5950

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 这题对我来说太经典了,估计我整个ICPC生涯都不会忘了这题。因为做过类似的题,所以刚开始就推出了转移方程,即D[i]保存以i为终点的所有路径上最小载重值最大的点,更新方程就是if D[i] < min(D[s],COST[s][i]) then D[i] = min(D[i] + COST[s][i]).我用dijkstra来做,但是WA了,无奈上网看了看,发现方程是对的,于是又用bellman写了一遍,果然A了。于是我就怀疑是不是我dijkstra理解搓了,果断找出MIT的公开课又学了一遍,发现理解是对的,顺便学会了对S的证明,此题可证出加入S的点已经正确。然后就开始了为期两天的DEBUG工程,调得快崩溃了,证了无数遍改了无数遍,终于在今天跑出了一组错误的数据。最后发现,问题出在优先队列上。
我优先队列里保存的是顶点的编号,然后通过比较D值来维护。于是,就在这里,出现了一个惊天地泣鬼神的错误。如果顶点2被加入到了队列里,并且此时的D值等于10,那么当它后来再次被更新以后,比如D值更新到了8,此时再次push的话,是push不进去的!队列会认为此元素已经存在,所以不做任何反应,虽然它的键值已经改变!网上一查果然有人遇到了同样的问题,他描述的比我清楚,传送门http://bbs.byr.cn/#!article/ACM_ICPC/8739?p=1 ,里面的第二个例子。我后来采用了3楼的办法,同时保存顶点号与D值,这样即使顶点号相同,但D值不同的话依然可以入队。
顺便一说,前面几题我用的是保存顶点的方法,所以虽然A了但是其实是错的。
印象实在太深了,这是我调试得最深入的一题,记录留念!
 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
int N;
int D[SIZE];
bool S[SIZE]; struct NNode
{
int pos,dis;
bool operator <(const NNode & r) const
{
return dis < r.dis;
};
};
struct Node
{
int vec,cost;
};
vector<Node> G[SIZE]; void Dijkstra(int);
int main(void)
{
//freopen("out.txt","r",stdin);
//freopen("2.txt","w",stdout);
int n,m,from;
int count = ;
Node temp; scanf("%d",&n);
while(n --)
{
scanf("%d%d",&N,&m);
for(int i = ;i <= N;i ++)
G[i].clear();
while(m --)
{
scanf("%d%d%d",&from,&temp.vec,&temp.cost);
G[from].push_back(temp);
swap(from,temp.vec);
G[from].push_back(temp);
}
Dijkstra();
printf("Scenario #%d:\n",++ count);
printf("%d\n\n",D[N]); } return ;
} void Dijkstra(int s)
{
NNode temp; priority_queue<NNode> que;
fill(S,S + SIZE,false);
fill(D,D + SIZE,);
D[s] = INF;
temp.pos = s;
temp.dis = INF;
que.push(temp); while(!que.empty())
{
NNode cur = que.top();
que.pop();
S[cur.pos] = true;
if(cur.pos == N)
break; for(int i = ;i < G[cur.pos].size();i ++)
if(!S[G[cur.pos][i].vec] && D[G[cur.pos][i].vec] < min(D[cur.pos],G[cur.pos][i].cost))
{
D[G[cur.pos][i].vec] = min(D[cur.pos],G[cur.pos][i].cost);
temp.pos = G[cur.pos][i].vec;
temp.dis = D[G[cur.pos][i].vec];
que.push(temp); //如果只保存顶点号的话会出错
}
}
}

Dijkstra

 #include <iostream>
#include <cstdio>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
struct Node
{
int from,to,cost;
}G[SIZE * SIZE];
int N,M;
int D[SIZE]; void Bellman_ford(int);
int main(void)
{
int n,m;
int count = ; scanf("%d",&n);
while(n --)
{
scanf("%d%d",&N,&M);
int temp = M;
int i = ;
while(temp --)
{
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
i ++;
G[i].from = G[i - ].to;
G[i].to = G[i - ].from;
G[i].cost = G[i - ].cost;
i ++;
}
Bellman_ford();
printf("Scenario #%d:\n",++ count);
printf("%d\n\n",D[N]);
} return ;
} void Bellman_ford(int s)
{
fill(D,D + SIZE,);
D[s] = INF;
for(int j = ;j < N - ;j ++)
{
bool update = false;
for(int i = ;i < M * ;i ++)
if(D[G[i].to] < min(D[G[i].from],G[i].cost))
{
D[G[i].to] = min(D[G[i].from],G[i].cost);
update = true;
}
if(!update)
break;
}
}

Bellman_Ford

POJ 1797 Heavy Transportation (最短路)的更多相关文章

  1. POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)

    题目:click here 题意: 有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量.分析: 其实这个求最大边可以 ...

  2. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  3. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  4. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  5. POJ 1797 Heavy Transportation

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

  6. POJ 1797 Heavy Transportation SPFA变形

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

  7. POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  8. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  9. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

随机推荐

  1. Firefox常用插件

    一.Web浏览使用插件 1.Adblock Plus广告拦截插件:能够自动拦截很多弹出广告,同时支持右键拦截指定信息 2.惠惠购物助手支持各大购物网站商品实时价格比较,非常棒的网站购物利器,插件下载地 ...

  2. MySQL 日期时间

    NOW()函数以`'YYYY-MM-DD HH:MM:SS'返回当前的日期时间,可以直接存到DATETIME字段中.CURDATE()以’YYYY-MM-DD’的格式返回今天的日期,可以直接存到DAT ...

  3. CSS3- px、em、rem区别介绍

    PX px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em ...

  4. Builder

    Builder模式的使用情景 相同的方法, 不同的执行顺序, 产生不同的事件结果 多个部件或零件, 都可以装配到一个对象中, 但是产生的运行结果又不相同 产品类比较复杂, 或者产品类中的调用顺序不同产 ...

  5. Extjs datefield 日历控件中文显示

    原版的日历控件选择的时候是英文的,不是中文的.后来将在extjs包中src下locale下ext-lang-zh_CN.js引用进来就汉化了

  6. win7和linux下的文件共享

    在vmware虚拟机下安装linux系统,如果自个电脑的win7设置成自动获取IP的话,每次使用FTP文件传输服务器都要检查win7和linux系统的IP是否处于同一网段,如果不是还要手动设置.再有一 ...

  7. ASP.NET中上传并读取Excel文件数据

    在CSDN中,经常有人问如何打开Excel数据库文件.本文通过一个简单的例子,实现读取Excel数据文件. 首先,创建一个Web应用程序项目,在Web页中添加一个DataGrid控件.一个文件控件和一 ...

  8. IE10、IE11出现“__doPostBack未定义”的解决办法。

    方法一:浏览器设置成兼容模式,这个是超级掩耳盗铃方法,你就没想过其他人也会出这个问题. 方法二.安装服务器版的.Net40的补丁.http://download.csdn.net/detail/565 ...

  9. dsPIC33EP ADC模块初始化及应用实例

    //文件名 p33adc.h #ifndef _P33ADC_H_ #define _P33ADC_H_ //#include "p33adc.h" //--AD1CON1 #de ...

  10. 为您的Android,iOS等应用加入声波传输功能

    记得12年左右的时候,美国出现了chirp应用,该应用能够使用声波在iphone手机间传输文本,图片.甚至视频.当时认为非常高大上. 再后来,到13年的时候国内也出现了非常多声波应用.比方支付宝的声波 ...