SDNU 1505.C.Proxy(迪杰斯特拉)

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

思路:这道题一看就是最短路径标记,我一开始直接在外面写了一个迪杰斯特拉函数,但是T了,于是就把迪杰斯特拉函数放在main函数内,但是在循环的地方我又写错了,搞了整整一天,整个人心态爆炸。
///
///                            _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;
}

 

转载于:https://www.cnblogs.com/RootVount/p/11248315.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
疫情居家办公系统管理系统按照操作主体分为管理员和用户。管理员的功能包括办公设备管理、部门信息管理、字典管理、公告信息管理、请假信息管理、签到信息管理、留言管理、外出报备管理、薪资管理、用户管理、公司资料管理、管理员管理。用户的功能等。该系统采用了MySQL数据库,Java语言,Spring Boot框架等技术进行编程实现。 疫情居家办公系统管理系统可以提高疫情居家办公系统信息管理问题的解决效率,优化疫情居家办公系统信息处理流程,保证疫情居家办公系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理公告,管理疫情居家办公系统信息,包括外出报备管理,培训管理,签到管理,薪资管理等,可以管理公告。 外出报备管理界面,管理员在外出报备管理界面中可以对界面中显示,可以对外出报备信息的外出报备状态进行查看,可以添加新的外出报备信息等。签到管理界面,管理员在签到管理界面中查看签到种类信息,签到描述信息,新增签到信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值