问题 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. BZOJ 2500 幸福的道路(race) 树上直径+平衡树

    structHeal { priority_queue<int> real; priority_queue<int> stack; void push(int x){ real ...

  2. CSS3不遥远,几个特性你要知道

    CSS是众所周知且应用广泛的网站样式语言,在它的版本三(CSS3)计划中,新增了一些能够节省时间的特性.尽管只有当前最新了浏览器版本才能支持这些 效果,但了解它们还是必须且很有趣味性的.CSS3中的5 ...

  3. MySQL的字符集小结

    正确了解MySQL的字符集问题,能够从根本上解决乱码的困扰. 首先,MySQL的字符集问题主要是两个概念,一个是Character Sets,一个是Collations,前者是字符内容及编码,后者是对 ...

  4. URAL1277 Cops and Thieves(最小割)

    Cops and Thieves Description: The Galaxy Police (Galaxpol) found out that a notorious gang of thieve ...

  5. ng的ngModel用来处理表单操作

    https://segmentfault.com/a/1190000009126012

  6. ionic改tab文字和icon图片的颜色

    The official way would be: Change in your theme/variables.scss Active icon for tabs on android is: $ ...

  7. Spring学习-- Bean 的作用域

    Bean 的作用域: 在 Spring 中 , 可以在 <bean> 元素的 scope 属性里设置 bean 的作用域. 默认情况下 , Spring 只为每个在 IOC 容器里声明的 ...

  8. 【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]

    消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一场战争中,战场由n个岛屿和n-1 ...

  9. 包与time,datetime,random,sys,shutil 模块

    一.包 包是什么? 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 注意: 1. 在python3中,即使包下没有__init__.py文件,import 包仍然不会报错,而在py ...

  10. elasticsearch.helpers.ScanError: Scroll request has only succeeded on xx shards

    # 当index=''为空时出现此错误