lightoj 1099 - Not the Best(次短路)

1099 - Not the Best
Time Limit: 2 second(s)Memory Limit: 32 MB

Robin has moved to a small village and sometimes enjoys returning to visit one of his best friends. He does not want to get to his old home too quickly, because he likes the scenery along the way. He has decided to take the second-shortest rather than the shortest path. He knows there must be some second-shortest path.

The countryside consists of R bidirectional roads, each linking two of the N intersections, conveniently numbered from 1 to N. Robin starts at intersection 1, and his friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains two integers N (1 ≤ N ≤ 5000) and R (1 ≤ R ≤ 105). Each of the next R lines contains three space-separated integers: u, v and w that describe a road that connects intersections u and v and has length w (1 ≤ w ≤ 5000).

Output

For each case, print the case number and the second best shortest path as described above.

Sample Input

Output for Sample Input

2

3 3

1 2 100

2 3 200

1 3 50

4 4

1 2 100

2 4 200

2 3 250

3 4 100

Case 1: 150

Case 2: 450



/*
    思路:
        把求最短路时更新最短路的那部分改一下。
        dis1,dis2数组分别记录到该点的最短路和次短路
        分三种情况:
            1.若该点最短路+下一条边比到下个点的最短路短,则更新下个点的最短路,同时更新次短路为原最短路
            2.若该点次短路+下一条边比到下个点的次短路短,则更新下个点的次短路
            3.若该点最短路+下一条边比到下个点的最短路长同时比下个点的次短路短,则更新下个点的次短路

*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
int n, m, k, head[maxn], dis1[maxn], dis2[maxn];
bool book[maxn];
struct node
{
    int v, w, next;
}edge[maxn];

void addEdge(int u, int v, int w)
{
    edge[k].v = v;
    edge[k].w = w;
    edge[k].next = head[u];
    head[u] = k++;
}

void spfa(int u)
{
    memset(dis1, INF, sizeof(dis1));
    memset(dis2, INF, sizeof(dis2));
    memset(book, 0, sizeof(book));
    queue<int> q;
    q.push(u);
    dis1[u] = 0;
    book[u] = 1;
    while(!q.empty())
    {
        u = q.front(); q.pop();
        book[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            int w = edge[i].w;
            if(dis1[v] > dis1[u]+w)
            {
                dis2[v] = dis1[v];
                dis1[v] = dis1[u]+w;
                if(!book[v]) book[v] = 1, q.push(v);
            }
            if(dis2[v] > dis2[u]+w)
            {
                dis2[v] = dis2[u]+w;
                if(!book[v]) book[v] = 1, q.push(v);
            }
            if(dis1[v] < dis1[u]+w && dis2[v] > dis1[u]+w)
            {
                dis2[v] = dis1[u]+w;
                if(!book[v]) book[v] = 1, q.push(v);
            }
        }
    }
}

int main(void)
{
    int t, ca = 1;
    cin >> t;
    while(t--)
    {
        k = 0;
        memset(head, -1, sizeof(head));
        scanf("%d%d", &n, &m);
        while(m--)
        {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            addEdge(u, v, w);
            addEdge(v, u, w);
        }
        spfa(1);
        printf("Case %d: %d\n", ca++, dis2[n]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值