问题 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. 【题解】[USACO12JAN]视频游戏的连击Video Game Combos

    好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...

  2. Java的Properties使用及格式定义

    java.util.Properties extends Hashtable<Object,Object> 方便读取 键值对 格式的文本资源工具 常用方法一览 初始化对象 new Prop ...

  3. 从零开始学习MXnet(二)之dataiter

    MXnet的设计结构是C++做后端运算,python.R等做前端来使用,这样既兼顾了效率,又让使用者方便了很多,完整的使用MXnet训练自己的数据集需要了解几个方面.今天我们先谈一谈Data iter ...

  4. 原生toolbar基本使用教程

    1.先写布局文件 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:title=" ...

  5. Hadoop NameNode元数据相关文件目录解析

    在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...

  6. 解析 Array.prototype.slice.call(arguments,0)

    Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...

  7. Android推送使用--文章集锦

    Android之基于百度云推送IM Android实现推送方式解决方案 Android消息推送(一)--AndroidPn(XMPP协议)Demo版到正式上线 采用XMPP协议实现Android推送 ...

  8. Vue 还是 React 还是 Angular ?

    有空的时候还是把3个都熟悉一下.除了Angular学习起来笔记花时间外.React跟Vue没啥难度,学习用的时间不多. 如果你在Google工作:Angular如果你喜欢TypeScript:Angu ...

  9. 最短路算法详解(Dijkstra,Floyd)

    最短路径 在一个无权的图中,若从一个顶点到另一个顶点存在着一条路径,则称该路径长度为该路径上所经过的边的数目,它等于该路径上的顶点数减1.由于从一个顶点到另一个顶点可能存在着多条路径,每条路径上所经过 ...

  10. bzoj 1041 数学推理

    原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1041 我们只需要求第一象限内(不包括坐标轴)的点数然后ans=ans*4+4就好了 首先我 ...