poj 3255

来源:互联网 发布:淘宝怎么买弹簧刀 编辑:程序博客网 时间:2024/06/06 06:45
POJ - 3255点我点我:-)


Roadblocks
Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

SubmitStatus

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 intersectionN.

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 andR
Lines 2.. R+1: Each line contains three space-separated integers: A,B, andD that describe a road that connects intersectionsA andB and has lengthD (1 ≤ D ≤ 5000)

Output

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

Sample Input

4 41 2 1002 4 2002 3 2503 4 100

Sample Output

450


求次短路。。。dijstra算法好。。。短。。。

这个做法就是多加了个d2[],求次短路。。

k短。。。不是很清楚,本星期搞定吧

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespace std;#define MAXN (5000+5)#define INF 0x3f3f3f3ftypedef pair<int, int> P;struct node{    int v, dis;};int n;vector<node> G[MAXN];int d[MAXN], d2[MAXN];void dijstra(int s){     priority_queue<P, vector<P>, greater<P> > q;     for(int i = 1; i <= n; i++) d[i] = d2[i] = INF;          d[s] = 0;     q.push(P(d[s], s));          while(!q.empty()){         P now = q.top(); q.pop();         int u = now.second, dis = now.first;         if(d2[u] < dis) continue;                  for(int i = 0; i < G[u].size(); i++){             int v = G[u][i].v, c2 = dis+G[u][i].dis;                          if(d[v] > c2){                 swap(d[v], c2);                 q.push(P(d[v], v));             }             if(d2[v] > c2 && d[v] < c2){                 d2[v] = c2;                 q.push(P(d2[v], v));             }         }     }}int main(){    freopen("test.in", "r", stdin);    freopen("test.out", "w", stdout);        int m;    scanf("%d%d", &n, &m);        for(int i = 1; i <= m; i++){        int u, v, dis;        scanf("%d%d%d", &u, &v, &dis);                G[u].push_back((node){v, dis});        G[v].push_back((node){u, dis});    }        dijstra(1);        printf("%d\n", d2[n]);        return 0;}


COME ON!


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 索尼手机密码忘了怎么办 索尼笔记本密码忘了怎么办 索尼手机解锁密码忘了怎么办 索尼记录仪密码忘了怎么办 索尼z3手机忘记开机密码怎么办 索尼忘记锁屏密码怎么办 索尼手机忘记锁屏密码怎么办 忘了手机解锁图案怎么办 索尼手机忘记开机密码怎么办 索尼笔记本开机密码忘了怎么办 手机的开机密码忘了怎么办 联想手机开机密码忘了怎么办 红米note3忘记开机密码怎么办 小米2忘了密码怎么办 小米笔记本电脑忘记开机密码怎么办 小米笔记本忘记开机密码怎么办 小米手机儿童模式忘记密码怎么办 小米应用锁密码忘了怎么办 小米air密码忘了怎么办 小米4密码忘了怎么办 小米手机开机密码忘了怎么办? 小米笔记本电脑开机密码忘了怎么办 小米笔记本开机密码忘了怎么办 htc手机忘记解锁图案怎么办 红米手机屏幕锁定怎么解锁怎么办 小米5s有id怎么办 手机密码找不回来了怎么办? 手机密码图案忘了怎么办 手机屏幕图案锁忘了怎么办 捡到苹果7有id锁怎么办 魅族什么都忘了怎么办 海信电视百事通登陆失败怎么办 去哪儿换号了怎么办 ipan充不进去电怎么办 安卓数据线松了怎么办 索尼z5耳机掉漆怎么办 索尼z5无限重启怎么办 苹果8基带坏了怎么办 oppo手机忘记图案密码怎么办 电池充不进去电怎么办 电脑充不进去电怎么办