hdu6181(次短路模板+spfa)

参考博客:https://blog.csdn.net/exp1ore/article/details/77541302

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181

Two Paths
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 2239    Accepted Submission(s): 862


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

输入:第一行一个数t表示t组测试样例,每组样例第一行两个数n,m表示有n个点,m条边(双向边),接下来m行每行三个数a,b,c表示a和b之间有一条长度为c的边

输出:1到n的次短路的距离

思路:1到n的次短路=min(1到u的最短路+dis[u,v]+v到n的最短路)且一定大于1到n的最短路

注意:给disu和disv初始化的时候初始化为0x3f3f3f3f3f3f3f3f(8个3f),一开始因为这个wa了十几发

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
#define LL long long
const int MOD=998244353;
const int inf=0x3f3f3f3f;
const LL inff=0x3f3f3f3f3f3f3f3f;
const LL MAX_N=100005;
const LL MAX_M=100005;
#define MEF(x) memset(x,-1,sizeof(x))
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
struct LX
{
    int v,next;
    LL di;
}lx[MAX_M*2];
int first[MAX_N];
int vis[MAX_N];
LL disv[MAX_N],disu[MAX_N];
queue<int> q;
void add(int u,int v,LL di,int cnt)
{
    lx[cnt].v=v;
    lx[cnt].di=di;
    lx[cnt].next=first[u];
    first[u]=cnt;
}
void spfa(int xx,LL dis[])
{
    ME0(vis);
    dis[xx]=0;
    q.push(xx);
    vis[xx]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;
        for(int i=first[x];i!=-1;i=lx[i].next)
        {
            int y=lx[i].v;
            if(dis[y]>dis[x]+lx[i].di)
            {
                dis[y]=dis[x]+lx[i].di;
                if(!vis[y])
                {
                    q.push(y);
                    vis[y]=1;
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int t1=1;t1<=t;t1++)
    {
        int n,m,a,b;
        LL c;
        scanf("%d%d",&n,&m);
        MEF(first);
        int cnt=0;
        for(int m1=1;m1<=m;m1++)
        {
            scanf("%d%d%lld",&a,&b,&c);
            add(a,b,c,++cnt);
            add(b,a,c,++cnt);
        }
        for(int n1=0;n1<=n;n1++)
        {
            disv[n1]=disu[n1]=inff;
        }
        spfa(1,disv);
        spfa(n,disu);
        LL ans=inff;
        for(int n1=1;n1<=n;n1++)
        {
            for(int i=first[n1];i!=-1;i=lx[i].next)
            {
                int n2=lx[i].v;
                LL sum=lx[i].di+disv[n1]+disu[n2];
                if(sum>disv[n]&&sum<ans)
                {
                    ans=sum;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值