Proxy(2016山东省省赛C)(最短路)(spfa)

问题 C: Proxy

时间限制: 2 Sec  内存限制: 128 MB
提交: 17  解决: 5
[提交][状态][讨论版]

题目描述

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?

输入

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.

输出

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).

样例输入

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

样例输出

3
-1
0
1【分析】要是只求最短距离那还简单,Spfa就挺好,但是要求输出你走的第一个点就有点麻烦,我们可以用一个pre数组,pre[i]存的是当走到i节点时,从0到i的最短路中与0相连的那个节点,这样在找最短路时就可以灵活的更新了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
#define RANGE 1005
#define MAX 0x3f3f3f3f
int cost[RANGE][RANGE];
int d[RANGE];
bool used[RANGE];
int n,m;
int pre[1005];
void spfa(int s)
{
    int i,j,now;
    for(i=0;i<=n+1;i++)
    {
        d[i]=MAX;
        used[i]=false;
    }
    int flag=0;
    for(i=0;i<=n+1;i++)
    {
        if(cost[0][i]<MAX)pre[i]=i;//初始化pre数组
    }
    used[s]=true;
    d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(i=0;i<=n+1;i++)
        {
            if(d[i]>d[now]+cost[now][i])
            {
                d[i]=d[now]+cost[now][i];
                if(used[i]==0)
                {
                    if(now!=0)pre[i]=pre[now];
                    used[i]==true;
                    q.push(i);
                }
            }
            if(d[i]==d[now]+cost[now][i])
            {
                    if(now!=0)pre[i]=min(pre[i],pre[now]);
            }
        }
    }
}
int main()
{
    int i,j,a,b,c;
    int we;
    scanf("%d",&we);
    while(we--)
    {scanf("%d%d",&n,&m);
        if(!n&&!m) break;
        for(i=0;i<=n+1;++i)
        {
            for(j=0;j<=i;j++)
            {
                if(i==j)  cost[i][j]=0;
                else  cost[i][j]=cost[j][i]=MAX;
            }
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            cost[a][b]=c;
        }
        spfa(0);
        if(d[n+1]<MAX)
        {
            if(pre[n+1]==n+1)printf("0\n");
            else printf("%d\n",pre[n+1]);
        }
        else printf("-1\n");
    }
    return 0;
}

				
时间: 2024-10-01 00:28:55

Proxy(2016山东省省赛C)(最短路)(spfa)的相关文章

2016山东省机器人比赛避障组小结

前言: 2016山东省机器人比赛差不多过去一年了,及时总结一下,给后来人一些经验,下文基本没有逻辑,全凭回忆,仅供参考,抛砖引玉. 比赛时间: 务必在7.10日之前完成报名,加入2017山东省机器人比赛官群和山科大机器人比赛官群,并及时确认报名是否完成.校赛2016年是国庆回来之后的第一个周,2016省赛好像是国庆一个月之后,可以看官方报名通知. 比赛准备: 工具:尖嘴钳.螺丝刀.万用表.剥线钳.电烙铁.焊锡等等 材料:常见的电阻电容等元器件.合适的电机.小车后轮(推荐跟买的电机配套的,务必要大

sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst

sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)

Pixel density Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Pixels per inch (PPI) or pixel density is a measurement of the resolution of devices in various contexts; typically computer displays, image scanners, and digital camera image s

sdut 2416:Fruit Ninja II(第三届山东省省赛原题,数学题)

Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game named "Fruit Ninja"?Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for Nvidia Tegra 2 based Android devices) is a video game de

2016年省赛总结

省赛,我和大三的学姐学长三人队,总体来说,我们队应该是银牌靠前的位置,这是我第一次参加山东省省赛,总体来说,今年的题目不是很难,很遗憾,我们的配合不是很好,过6题,拿了铜牌第一,这场比赛我存在几个失误,导致我们连正常水平都没有发挥,大体总结下面几条:1.略微紧张,导致第一道水题wa了一次  2.配合不好,我们三个一起练习的时间很少,省赛前的一段时间很少做题,导致做题并没有太好的感觉  3.最后一个小时,心态爆炸,我们队最后写出三道题,3wa,改了好久没过,我的简单的记忆化搜索就差一点,当时就是不

sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess set out immedia

2016 CCPC 网络赛 B 高斯消元 C 树形dp(待补) G 状压dp+容斥(待补) H 计算几何

2016 CCPC 网络赛 A - A water problem 水题,但读题有个坑,输入数字长度很大.. B - Zhu and 772002 题意:给出n个数(给出的每个数的质因子最大不超过2000),选出多个数相乘得b.问有多少种选法让b 为完全平方数. tags:高斯消元,求异或方程组解的个数.   好题 每个数先素数分解开.  对于2000以内的每个素数p[i],这n个数有奇数个p[i]则系数为1,偶数个则系数为0,最后n个数的p[i]系数异或和都要为0才会使得最后的积为完全平方数.

2016 华科校赛 B. And

// 2016 华科校赛 B. And http://acm.hust.edu.cn/problem/show/1672 题目描述 给出 a[1], ..., a[n].查询 t[1], ... t[m],问有多少 a[] 的子序列的按位与是 t[]. 输入 多组测试,EOF 结束. n a[] m t[] 1 ≤ n ≤ 1e6 1 ≤ m ≤ (1 << 20) 1 ≤ a[i] ≤ (1 << 20) 1 ≤ t[i] ≤ (1 << 20) 输出 输出一行,每组

HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fail$指针往下走,当匹配成功的时候更新$f[i]$ $f[i]$表示要屏蔽以第$i$个字母结尾的长度为$f[i]$的字符串. 原文地址:https://www.cnblogs.com/cxhscst2/p/8452147.html