HDU 6181 次短路(K短路)

Two Paths

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

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

才讲的K短路,第二天多校就考到这个。=-=

比较裸,直接用A*算法+Dij堆优化,不加堆优化的Dij会超时的,而且这个题数据边的w值会很大,所以所有的边的变量,中间累加的变量,数组存下的长度,最后的A*函数返回结果(一开始WA了很多次,就在这里,返回值也要long long,只顾改了变量了,白白WA了几发)都要设置成long long,第二个就是无向边了,建图和反向建图的两个数组都要push两次。这也是一开始反向建图少一个就WA。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <cstdio>
#define MAXN 100010
#define INF (1LL<<62)
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
int N, M, S, T, K;
ll dist[MAXN];
ll tdist[MAXN];
int cnt[MAXN];
bool f[MAXN];
vector<P> Adj[MAXN];
vector<P> Rev[MAXN];
struct Edge {
    int to;
    ll len;
    Edge(){}
    Edge(int t, ll l):to(t), len(l){}
};
priority_queue<Edge> q;

bool operator<(const Edge &a, const Edge &b) {
    return (a.len + dist[a.to]) > (b.len + dist[b.to]);
}

void dijkstra() {
    memset(dist, 0, sizeof(dist));
    fill(tdist, tdist+MAXN, INF);
    tdist[T] = 0;
    while(!q.empty()) q.pop();
    q.push(Edge(T, 0));
    while (!q.empty()) {
        int x = q.top().to;
        ll d = q.top().len;
        q.pop();
        if (tdist[x] < d) continue;
        for (int i = 0; i < Rev[x].size(); i++) {
            int y = Rev[x][i].first;
            ll len = Rev[x][i].second;
            if (d+ len < tdist[y]) {
                tdist[y] = d + len;
                q.push(Edge(y, tdist[y]));
            }
        }
    }
    for (int i = 1; i <= N; i++){
        dist[i] = tdist[i];
    }
}

//注意这里是long long的返回结果啊!
ll aStar() {
    if (dist[S] == INF) return -1;
    while (!q.empty()) q.pop();
    q.push(Edge(S, 0));
    memset(cnt, 0, sizeof(cnt));
    while (!q.empty()) {
        int x = q.top().to;
        ll d = q.top().len;
        q.pop();
        cnt[x]++;
        if (cnt[T] == K) return d;
        if (cnt[x] > K) continue;
        for (int i = 0; i < Adj[x].size(); i++) {
            int y = Adj[x][i].first;
            ll len = Adj[x][i].second;
            q.push(Edge(y, d+len));
        }
    }

    return -1;
}

int main() {
//    freopen("in.txt","r",stdin);
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int tt;
    cin>>tt;
    while(tt--) {
    //初始化,忘了就WA
    for(int i = 0; i <= N; i++) {
        Adj[i].clear();
        Rev[i].clear();
    }
    int a, b;
    ll t;
    cin >> N >> M;
    for (int i = 0; i < M; i++) {
        cin >> a >> b >> t;
        Adj[a].push_back(make_pair(b, t));
        Adj[b].push_back(make_pair(a, t));
        Rev[b].push_back(make_pair(a, t));
        Rev[a].push_back(make_pair(b, t));
    }
    S = 1;
    T = N;
    K = 2;
//    cin >> S >> T >> K;

    if (S == T) K++;

    dijkstra();
    cout << aStar() << endl;
    }
    return 0;
}

次短路做法,用的别人模版:

原理就是在存dis最短路的时候同时用另外一个数组存次短路,在最短路交换边和dis[]值得时候,次短路存下新交换的没最短边长的值即可。

#include<cstdio>
#include<cstring>
#include<queue>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long int ll;
#define MAXM  350010
#define MAXN 250010
#define INF 1223372036854775807
typedef pair<ll,int> pp;

typedef struct node
{
    int v;
    ll w;
    node(int tv=0,ll tw=0):
        v(tv),w(tw){};
}node;

int n,m;
vector<node> edge[MAXM];
ll dis[MAXN],dis2[MAXN];   //dis[i]表示最短路,dis2[i]表示次短路

void solve()
{
    fill(dis+1,dis+n+1,INF);
    fill(dis2+1,dis2+n+1,INF);

    priority_queue<pp, vector<pp>, greater<pp> > q;  //用优先队列加速搜索
    dis[1]=0;
    q.push(pp(dis[1],1));   //second是该边指向(虽然是无向的)的顶点,first是这条边的权值
    while(q.size())
    {
        pp p=q.top(); q.pop();
        int v=p.second;
        ll d=p.first;
        if(dis2[v]<d) continue;  //如果当前取出的值不是到v的最短路或次短路就contniue,因为v->e的最短边和次短边一定是由->v的最短边和次短边+edge(v,e)得到
        for(int i=0;i<edge[v].size();i++)
        {
            int e=edge[v][i].v;
            ll d2=d+edge[v][i].w;
            if(dis[e]>d2)
            {
                swap(dis[e],d2);
                q.push(pp(dis[e],e));
            }
            if(dis2[e]>d2&&d2>dis[v])  //d2>dis[v]防止d2小于dis[v],这样v->e就变成负权边了,但可有可无
            {
                dis2[e]=d2;
                q.push(pp(dis2[e],e));
            }
        }
    }
    printf("%lld\n",dis2[n]);

}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) edge[i].clear();
        for(int i=0;i<m;i++)
        {
            int a,b;
            ll c;
            scanf("%d%d%lld",&a,&b,&c);
            edge[a].push_back(node(b,c));
            edge[b].push_back(node(a,c));
        }
        solve();
    }
}
时间: 2024-11-06 23:46:35

HDU 6181 次短路(K短路)的相关文章

最短路 次短路 k短路(k很小)

最短路 luogu 3371 https://www.luogu.org/problemnew/show/P3371 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 const int maxn=1e4+10; 8 9 int dist[m

2017多校第10场 HDU 6181 Two Paths 次短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证明这题数据还是水了.下来我修改了一下次短路的,就可以避免这种情况了.提供一个sample 4 4 (1,2,1)( 1, 3,1) (2 4,1) (3 ,4,1).这组的正确答案是2,算法就来看代码吧. #include <bits/stdc++.h> using namespace std;

HDU 2485 求删最少点使得 边权=1的有向图最短路&gt;k

题意: 给定n个点 m条有向边 k 下面m条有向边 问删最少几个点使得1-n的最短路>k 10 11 5 1 2 2 3 3 4 4 5 5 10 2 9 1 6 6 7 7 8 8 9 9 10 8 10 5 1 2 2 3 3 4 4 5 5 6 6 8 1 7 7 8 4 7 7 4 #include <stdio.h> #include <string.h> #define N 55 #define INF 1<<30 #define eps 1e-5 i

HDU 3832 Earth Hour (最短路)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 1518    Accepted Submission(s): 607 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fun

HDU 4856 Tunnels (最短路+状压DP)

题意:给你N*N的网格,'.'表示可以走,'#'表示不能走,m条管道,每条管道有起点和终点坐标, Bob每次可以走到相邻的网格花费1s,问Bob走完m条管道要花多少时间:Bob在管道内不计算时间 即计算Bob从管道 i 的出口走到管道 j 的入口的时间Dis(e[i],s[j])的最小和,起点可以任意: 思路:看了题解说是状态压缩DP然后深入理解了下. 首先算出d[e[i]][s[j]]的最短距离,不能到达为-1: dp[i][j] : 表示以 j 为起点状态为 i 的最小值.其中 i 是用十进

HDU 2833 WuKong(floyd最短路)

题目地址:HDU 2833 这题想到了最后是通过dis[s][t]==dis[s][i]+dis[i][j]+dis[j][t]的思路来判定是否属于最短路的一条..但是没想到可以用floyd来找最短路中的点数...最短路还是太渣了..好多性质都不会利用.. 这题的思路就是通过floyd对每两个点之间的最短路条数进行计数,然后通过上面的公式(对两条路线均要判定,都符合才说明都可以走),再找最短路中的最大点数. 代码如下: #include <iostream> #include <stdi

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

HDU 2544 最短路 (最短路)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31955    Accepted Submission(s): 13845 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最

hdu 1690 Bus System(最短路)

问题: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 1000000000000 typedef __int64 LL; const int N = 110; __int64 dis[N][N],place[N]; __int64 L1,L2,L3,L4,C1,C2,C3,C4; int n,m