HDU

来源:互联网 发布:sat数学 知乎 编辑:程序博客网 时间:2024/06/15 20:04

Two Paths


Problem Description
You are given a undirected graph with n nodes (numbered from 1 to n) and m edges. Alice and Bob are now trying to play a game. 
Both of them will take different route from 1 to n (not necessary simple).
Alice always moves first and she is so clever that take one of the shortest path from 1 to n.
Now is the Bob's turn. Help Bob to take possible shortest route from 1 to n.
There's neither multiple edges nor self-loops.
Two paths S and T are considered different if and only if there is an integer i, so that the i-th edge of S is not the same as the i-th edge of T or one of them doesn't exist.
 

Input
The first line of input contains an integer T(1 <= T <= 15), the number of test cases.
The first line of each test case contains 2 integers n, m (2 <= n, m <= 100000), number of nodes and number of edges. Each of the next m lines contains 3 integers a, b, w (1 <= a, b <= n, 1 <= w <= 1000000000), this means that there's an edge between node a and node b and its length is w.
It is guaranteed that there is at least one path from 1 to n.
Sum of n over all test cases is less than 250000 and sum of m over all test cases is less than 350000.
 

Output
For each test case print length of valid shortest path in one line.
 

Sample Input
23 31 2 12 3 41 3 32 11 2 1
 

Sample Output
53
Hint
For testcase 1, Alice take path 1 - 3 and its length is 3, and then Bob will take path 1 - 2 - 3 and its length is 5.For testcase 2, Bob will take route 1 - 2 - 1 - 2 and its length is 3
 



题意:求真 · 次短路。一条路可以走多遍,如果最短路有多种,次短路其实就是另外一种最短路……WA的应该都是这里吧……还有就是INF不能用int的,要用LL的(其实应该是可以int的吧)……我感受到了出题者满满的恶意。


解题思路:套个次短路模板就好了,然后将里面的<号改成<=号。用SPFA会超时不知道为什么……还是我写搓了,改成了迪杰斯特拉就过了。思路其实就是,存最短路的同时存下次短路 ……


#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#define INF (1LL<<62)//WA了好几发#define ll long long intusing namespace std;typedef pair<ll,int>P;struct edge{    int to;    ll dis;    edge(int to,ll dis){        this -> to = to;        this -> dis = dis;    }};int N,R;int a,b;ll c;ll dis[100005];         //记录最短路径ll disc[100005];       //记录次短路径vector<edge>G[100005];void dijkstra(){    fill(dis,dis+100005,INF);    fill(disc,disc+100005,INF);    priority_queue<P,vector<P>,greater<P> >q;    dis[1]=0;    q.push(P(0,1));    while(q.size()){        P p=q.top();        q.pop();        ll dd=p.first;        int v=p.second;        if(disc[v]<dd) continue;        for(int i=0;i<G[v].size();i++){            edge& e=G[v][i];            ll d=dd+e.dis;            if(dis[e.to]>=d){                ll ttt=d;                d=dis[e.to];                dis[e.to]=ttt;                q.push(P(dis[e.to],e.to));            }            if(disc[e.to]>=d&&dis[e.to]<=d){                disc[e.to]=d;                q.push(P(disc[e.to],e.to));            }        }    }    cout<<disc[N]<<endl;}int main(){    int t;    scanf("%d",&t);    while(t--){        for(int i=0;i<100005;i++)            G[i].clear();    scanf("%d%d",&N,&R);    for(int i=1;i<=R;i++){        scanf("%d%d%lld",&a,&b,&c);        G[a].push_back(edge(b,c));        G[b].push_back(edge(a,c));    }    dijkstra();    }    return 0;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 房开逾期交房怎么办 买了保险想退保怎么办 辐射避难所探索废土死了怎么办 大门上边的齿轮滑丝怎么办 国通石油储油卡怎么办 买大棚房受骗了怎么办 朋友做安利天天来我门面怎么办 安利优惠顾客卡怎么办 苹果手机天气温度不显示怎么办? 安利净水器坏了怎么办 安利净水器滤芯盖搭配坏怎么办 安利会员卡过期了怎么办 婴儿吃了润唇膏怎么办? 用错沐浴露洗头怎么办 雅蜜润肤沐浴露怎么办 自煮小火锅水放少了怎么办 安利皇后锅发黑怎么办 宝宝灌肠后不拉屎怎么办 吃蛋白质粉肚子长胖了怎么办 安利产品过期了怎么办 拼多多拼不到人怎么办 被海南大宗骗了怎么办 手机被游戏扣钱怎么办 做酵素剩下的水果怎么办 喝了酵素胃疼怎么办 海科融通不到账怎么办 美团外卖没生意怎么办 淘宝联盟领券销售怎么办 微信返利被骗了怎么办 众筹失败后资金怎么办 健身房不给退卡怎么办 婆婆陷入民间传销组织怎么办 被三生公司骗了怎么办? ppt保存成了图片怎么办 苹果6速度变慢了怎么办 苹果6s速度很慢怎么办 微信支付上限了怎么办 佳享健康骗老人怎么办 宝宝吃了硅胶乳贴怎么办 用完卫生巾后阴部有些不舒服怎么办 指甲上有荧光剂怎么办