SDNU 1505.C.Proxy (Dijkstra)

Description

Because of the GFW (Great Firewall), we cannot directly visit many websites, such as Facebook, Twitter, YouTube, etc. But with the help of proxy and proxy server, we can easily get to these website.
    You have a list of several proxy servers, some of them can be connected directly but others can’t. But you can visit proxy servers through other proxy server by a one-way connection. 
    As we all know, the lag of internet visit will decide our feelings of the visit. You have a very smart proxy software which will find the least lag way to reach the website once you choose a directly reachable proxy server. 
    You know the lag of every connection. The lag of your visit is the all the lags in your whole connection. You want to minimize the lag of visit, which proxy server you will choose?

Input

  Multiple test cases, the first line is an integer T (T <= 100), indicating the number of test cases.
The first line of each test case is two integers N (0 <= N <= 1000), M (0 <= M <= 20000). N is the number of proxy servers (labeled from 1 to N). 
0 is the label of your computer and (N+1) is the label of the server of target website.
Then M lines follows, each line contains three integers u, v, w (0 <= u, v <= N + 1, 1 <= w <= 1000), means u can directly connect to v and the lag is w.

Output

 An integer in one line for each test case, which proxy server you will choose to connect directly. You can only choose the proxy server which can be connected directly from your computer.
If there are multiple choices, you should output the proxy server with the least label. If you can’t visit the target website by any means, output “-1” (without quotes). If you can directly visit the website and the lag is the least, output “0” (without quotes).

Sample Input

4
3 6
0 1 10
1 2 1
2 4 4
0 3 2
3 2 1
3 4 7
2 4
0 2 10
0 1 5
1 2 4
2 1 7
1 3
0 2 1
0 1 2
1 2 1
1 3
0 2 10
0 1 2
1 2 1

Sample Output

3 
-1 
0 
1 idea: a look at this question mark is the shortest path, I started out to write a Dijkstra function directly, but T, so put Dijkstra functions in the main function inside, but where the cycle I was wrong, do the whole day, the whole mentality explosion.


///
///                            _ooOoo_
///                           o8888888o
///                           88" . "88
///                           (| -_- |)
///                           O\  =  /O
///                        ____/`---'\____
///                      .'  \\|     |//  `.
///                     /  \\|||  :  |||//  \
///                    /  _||||| -:- |||||-  \
///                    |   | \\\  -  /// |   |
///                    | \_|  ''\---/''  |   |
///                    \  .-\__  `-`  ___/-. /
///                  ___`. .'  /--.--\  `. . __
///               ."" '<  `.___\_<|>_/___.'  >'"".
///              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
///              \  \ `-.   \_ __\ /__ _/   .-` /  /
///         ======`-.____`-.___\_____/___.-`____.-'======
///                            `=---='
///        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
///                      Buddha Bless, No Bug !
///

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int t, n, m, u, v, w, dis[1000+8][1000+8], len[1000+8], road[1000+8];
bool sign[1000+8];


int main()
{
    for(scanf("%d", &t); t--; )
    {
        scanf("%d%d", &n, &m);
        for(int i = 0; i <= n+1; i++)
            for(int j = 0; j <= n+1; j++)
                if(i == j)dis[i][j] = 0;
                else dis[i][j] = inf;
        for(int i = 0; i<m; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            dis[u][v] = w;
        }
        fill(len, len+n+2, inf);
        fill(sign, sign+n+2, 0);
        len[0] = 0;
        road[0] = -1;
        for(int i = 0; i<n+2; i++)
        {
            int pos, minn = inf;
            for(int j = 0; j<n+2; j++)
                if(!sign[j] && len[j] <= minn)
                {
                    minn = len[j];
                    pos = j;
                }
            sign[pos] = 1;
            for(int j = 0; j<n+2; j++)
                if(!sign[j] && len[j]>len[pos]+dis[pos][j])
                {
                    len[j] = len[pos]+dis[pos][j];
                    road[j] = pos;
                }
                else if(!sign[j] && len[j] == dis[pos][j]+len[pos])
                    road[j] = min(road[j], pos);
        }
        if(len[n+1] == inf)printf("-1\n");
        else if(len[n+1] == dis[0][n+1])printf("0\n");
        else
        {
            int k = n+1;
            while(road[k] != 0)
            {
                if(road[k] == 0)break;
                k = road[k];
            }
            printf("%d\n", k);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/RootVount/p/11248315.html