山东省第七届ACM省赛 Proxy(spfa输出路径)

13 篇文章 0 订阅
13 篇文章 0 订阅

Problem 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

题目描述

从标号0走到标号n+1的最短路,输出这条路上第一步到达的标号label,在所有满足条件的最短路中,输出标号最小的label.如果标号0和标号n+1直连且为最短路那么输出“0”;如果不可达输出-1.

解题思路

spfa求最短路并标记路径,注意对于最短路存在多种情况时对于最小标号更新的处理.

代码实现

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4+7;
const int maxm = 1e3+7;
#define INF 0x3f3f3f3f
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
struct node
{
    int u;
    int v;
    ll cost;
    int next;
}edge[maxn*2];
int maps[maxm][maxm];
int head[maxn],tot;
int vis[maxn],dis[maxn];
int path[maxn];   //标记源点s到i 的最短路径中,结点i的父节点
void init()
{
    memset(head,-1,sizeof(head));
    memset(path,-1,sizeof(path));  //初始化
    tot=0;
}
void addedge(int u,int v,int c)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].cost=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void print_path(int point)   //输出路径
{
    if(path[point]!=0)
        print_path(path[point]);
    if(path[point]==0)       //当前point的父节点为0,则point即为第一步走的节点
    {
        cout<<point<<endl;
        return ;
    }
}
ll spfa(int s,int t)
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    vis[s]=1;
    queue<int>qu;
    qu.push(s);
    while(!qu.empty())
    {
        int u=qu.front();
        qu.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                path[v]=u;        //标记父亲节点
                if(!vis[v])
                {
                    vis[v]=1;
                    qu.push(v);
                }
            }
            else if(dis[v]==dis[u]+edge[i].cost)   //最短路存在多组解
            {
                path[v]=min(path[v],u);            //更新标号的最小值
                if(!vis[v])
                {
                    vis[u]=1;
                    qu.push(v);
                }
            }
        }
    }
    return dis[t];
}
int main()
{
    IO;
    int T;
    int n,m;
    int u,v,w;
    cin>>T;
    while(T--)
    {
        init();
        cin>>n>>m;
        while(m--)
        {
            cin>>u>>v>>w;
            addedge(u,v,w);
            maps[u][v]=w;
        }
        int length=spfa(0,n+1);
        if(length==INF) cout<<"-1"<<endl;         //不可达
        else if(length==maps[0][n+1]) cout<<"0"<<endl;   //直达且为最短路
        else print_path(n+1);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值