HDU6181-Two Paths

Two Paths

                                                                     Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
                                                                                             Total Submission(s): 1036    Accepted Submission(s): 487


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
  
  
2 3 3 1 2 1 2 3 4 1 3 3 2 1 1 2 1
 

Sample Output
  
  
5 3
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
 

Source
 

题意:就是求一张图的次短路



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <unordered_map>
#include <functional>

using namespace std;

#define LL long long
const LL INF=0x3f3f3f3f3f3f3f3f;

int n,m;
int vis[100009],s[100009],nt[200009],e[200009];
LL dis[100009][2],v[200009];

struct node
{
    int id;
    LL x;
    bool operator<(const node &a)const
    {
        return x>a.x;
    }
}pre,nt1;

void Dijkstra()
{
    memset(vis,0,sizeof vis);
    memset(dis,INF,sizeof dis);
    pre.id=1,pre.x=0;
    dis[1][0]=0;
    priority_queue<node>q;
    q.push(pre);
    while(!q.empty())
    {
        pre=q.top();
        q.pop();
        vis[pre.id]++;
        for(int i=s[pre.id];~i;i=nt[i])
        {
            if(vis[e[i]]==2) continue;
            if(dis[e[i]][0]>pre.x+v[i])
            {
                dis[e[i]][1]=dis[e[i]][0];
                dis[e[i]][0]=pre.x+v[i];
                nt1.id=e[i],nt1.x=dis[e[i]][0];
                q.push(nt1);
            }
            else if(dis[e[i]][0]<=pre.x+v[i])
            {
                if(dis[e[i]][1]>pre.x+v[i])
                {
                    dis[e[i]][1]=pre.x+v[i];
                    nt1.id=e[i],nt1.x=dis[e[i]][1];
                    q.push(nt1);
                }
            }
        }
    }
    printf("%lld\n",dis[n][1]);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(s,-1,sizeof s);
        int cnt=0;
        for(int i=1;i<=m;i++)
        {
            int uu,vv;
            LL ww;
            scanf("%d%d%lld",&uu,&vv,&ww);
            nt[cnt]=s[uu],s[uu]=cnt,e[cnt]=vv,v[cnt++]=ww;
            nt[cnt]=s[vv],s[vv]=cnt,e[cnt]=uu,v[cnt++]=ww;
        }
        Dijkstra();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值