hdu 6181 (次短路

本文介绍了一种解决次短路径问题的有效算法,并通过一个具体的竞赛题目进行了解析。该算法能够在给定图中找到从起点到终点的次短路径长度,适用于图论和算法竞赛等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

Two Paths

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


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6181  6180  6179  6178  6177 


次短路不断更新最短的路径和次短的路径,到最后一定是次短路径加最短路径。

/*
裸的次短路
不断更新v->u的次短路,直到v->u的次短路只比最短路小
*/
#include <vector>
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
#define ll long long
#define INF 1e18
#define MAXM 100100
struct edge{int to;ll w;};
typedef pair<ll,int>P;
int n,r;
ll d[MAXM];
ll d1[MAXM];
vector<edge>G[MAXM];
bool vis[MAXM];
void solve()
{
    priority_queue<P, vector<P>,greater<P> >que;
    fill(d+1,d+n+1,INF);
    fill(d1+1,d1+n+1,INF);
    d[1]=0;
    que.push(P(0,1));
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(d1[v]<p.first)continue;
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            ll d2=p.first+e.w;
            if(d[e.to]>d2)
            {
                swap(d[e.to],d2);
                que.push(P(d[e.to],e.to));
            }
            if(d1[e.to]>d2&&d[e.to]<d2)
            {
                d1[e.to]=d2;
                que.push(P(d1[e.to],e.to));
            }
        }
    }
    printf("%I64d\n",d1[n]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&r);
        for(int i=1;i<=n;i++)
            G[i].clear();
        int u,v;
        ll w;
        while(r--)
        {
            scanf("%d %d %I64d",&u,&v,&w);
            G[u].push_back(edge{v,w});
            G[v].push_back(edge{u,w});
        }
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值