问题 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[];
void spfa(int s)
{
int i,j,now;
for(i=;i<=n+;i++)
{
d[i]=MAX;
used[i]=false;
}
int flag=;
for(i=;i<=n+;i++)
{
if(cost[][i]<MAX)pre[i]=i;//初始化pre数组
}
used[s]=true;
d[s]=;
queue<int> q;
q.push(s);
while(!q.empty())
{
now=q.front();
q.pop();
for(i=;i<=n+;i++)
{
if(d[i]>d[now]+cost[now][i])
{
d[i]=d[now]+cost[now][i];
if(used[i]==)
{
if(now!=)pre[i]=pre[now];
used[i]==true;
q.push(i);
}
}
if(d[i]==d[now]+cost[now][i])
{
if(now!=)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=;i<=n+;++i)
{
for(j=;j<=i;j++)
{
if(i==j) cost[i][j]=;
else cost[i][j]=cost[j][i]=MAX;
}
}
for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
cost[a][b]=c;
}
spfa();
if(d[n+]<MAX)
{
if(pre[n+]==n+)printf("0\n");
else printf("%d\n",pre[n+]);
}
else printf("-1\n");
}
return ;
}
												

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

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

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  2. 2016年省赛 G Triple Nim

    2016年省赛 G Triple Nimnim游戏,要求开始局面为先手必败,也就是异或和为0.如果n为奇数,二进制下最后一位只有两种可能1,1,1和1,0,0,显然异或和为1,所以方案数为0如果n为偶 ...

  3. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  4. L - Subway(最短路spfa)

    L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...

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

    Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game name ...

  6. ACM-ICPC 2016亚洲区域赛(沈阳站)游记(滚粗记)

    首发于QQ空间和知乎,我在这里也更一下.   前言 以前高中搞竞赛的时候,经常看到神犇出去比赛或者训练之后写游记什么的,感觉萌萌哒.但是由于太弱,就没什么心情好写.现在虽然还是很弱,但是抱着享受的心情 ...

  7. 2015沈阳区域赛Meeting(最短路 + 建图)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. 2016 长春东北赛---Coconuts(离散化+DFS)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5925 Problem Description TanBig, a friend of Mr. Frog ...

  9. 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...

随机推荐

  1. C# 获取ORACLE SYS.XMLTYPE "遇到不支持的 Oracle 数据类型 USERDEFINED"

    1.需要加函数 2.需要加表别名 select   a.XML.getclobval()  from TB1  a

  2. idea中如何配置git以及在idea中初始化git

    idea中如何配置git以及在idea中初始化git呢: 参考此博文: http://blog.csdn.net/qq_28867949/article/details/73012300 *为了这个问 ...

  3. react 记录:React Warning: Hash history cannot PUSH the same path; a new entry will not be added to the history stack

    前言: react-router-dom 4.4.2 在页面中直接使用 import { Link } from 'react-router-dom' //使用 <Link to={{ path ...

  4. django 连接 oracle 问题

    安装 oracle 后,在 django 项目中连接出现问题记录. 问题1:pip install cx_Oacle 未出现任何问题,但运行过程出现: 原因:连接 oracle 的工具 cx_Orac ...

  5. xcode 10 新特性

    这里主要介绍一下Xcode10 版本主要更新的内容.随着iOS12的发布,Xcode10已经可以从Mac App Store下载.Xcode10包含了iOS12.watchOS 5.macOS10.1 ...

  6. WebView使用--文章集锦

    对于android WebView加载不出Html5网页的解决方法 在android4.4中webview的使用相对于之前版本的一些区别 理解WebKit和Chromium: Android 4.4 ...

  7. centos yum 安装 mysql

      centos7下使用yum安装mysql 时间:2015-03-07 21:26:20      阅读:87445      评论:0      收藏:1      [点我收藏+] 标签: Cen ...

  8. 【数据结构】bzoj1651专用牛棚

    Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will onl ...

  9. JHDU 2601 An easy problem (数学 )

    title: An easy problem 数学 杭电2601 tags: [数学] 题目链接 Problem Description When Teddy was a child , he was ...

  10. 操作MySQL数据库相关代码

    注意事项: 1.导入驱动包,如我导的是mysql-connector-java-5.1.26-bin.jar 2.修改下面代码中的用户名和密码 3.其中URL为"jdbc:mysql://数 ...