poj 3255 Roadblocks(次最短路径)

来源:互联网 发布:学拼音打字软件下载 编辑:程序博客网 时间:2024/05/16 05:26

Roadblocks
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10836 Accepted: 3852

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Line 1: Two space-separated integers: N and R 
Lines 2..R+1: Each line contains three space-separated integers: AB, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node N

Sample Input

4 41 2 1002 4 2002 3 2503 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

题目大意:找到从1->N的次最短路径
次最短路的求解:
到某个顶点的次最短路
1、到其他某个顶点u的最短路+u->v的边
2、到某个定点的次最短路+u->v的边
不断更新最短/次最短路的数组


#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>#include <ctype.h>#include <algorithm>#include <map>#include <math.h>#include <stack>#include <queue>#define Max 6000#define inf 100000000using namespace std;int N,R;struct edge{    int to;    int cost;};vector<edge> G[Max];int dis[Max];int dis2[Max];typedef pair<int, int> P;void solve(){    priority_queue<P,vector<P>,greater<P> > q;    fill(dis, dis+N, inf);    fill(dis2, dis2+N, inf);    dis[0] = 0;    q.push(P(0,0));    while (!q.empty()) {        P p = q.top();q.pop();        int v = p.second, d = p.first;        if (dis2[v]<d) {            continue;        }        for (int i=0; i<G[v].size(); i++) {            edge &e = G[v][i];            int d2 = e.cost + d;            if (dis[e.to]>d2) {                swap(dis[e.to], d2);                q.push(P(dis[e.to],e.to));            }            if (dis2[e.to]>d2&&dis[e.to]<d2) {                dis2[e.to] = d2;                q.push(P(dis2[e.to],e.to));            }        }    }    printf("%d\n",dis2[N-1]);}int main(){    while (scanf("%d%d",&N,&R)!=EOF) {        for (int i=0; i<=N; i++) {            G[i].clear();        }        for (int i=0; i<R; i++) {            int s,e,c;            scanf("%d%d%d",&s,&e,&c);            s--,e--;            edge p;            p.to = e,p.cost = c;            G[s].push_back(p);            p.to = s;            G[e].push_back(p);        }        solve();    }    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 易信密码忘记了怎么办 滴滴永久封停的号怎么办 微信版本过低怎么办 报关时通关单号没录怎么办 公司注销了行政许可证怎么办呢 报关项数超过50项怎么办 出口报关件数报多了怎么办 关税少交被海关缉私查到怎么办 外贸报关hs编码报错怎么办 报关金额少报了怎么办 进口报关金额少报了怎么办 报关重量报少了怎么办 金额报多100倍怎么办 ems没扫描到单号怎么办 回国海关被税了怎么办 代购买错东西了怎么办 征信报告有逾期怎么办 evus信息填错了怎么办 清关一个月了该怎么办 清关一直不发怎么办 清关一个月了怎么办 香港ems到西安海关税怎么办 寄东西被海关扣留怎么办 天津港新舱单品名错误核销怎么办 移动手机不能用联通卡怎么办 移动手机联通卡网速卡怎么办 移动手机不支持联通4g怎么办 汽车分离轴承异响怎么办 至尊宝被冻结了怎么办 qq冻结了至尊宝怎么办 至尊宝没办法申诉怎么办 至尊宝qq被冻结怎么办 未满16岁怎么办手机卡 联想预装的office卸载怎么办 win10激活后无法启动怎么办 移动电话卡没用了没注销怎么办 快捷快递客服热线一直打不通怎么办 牛奶乳加钙咀嚼片吃多了怎么办 三生骗了我该怎么办 国珍产品新时代卡怎么办 三个月大的宝宝对眼怎么办