问题 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 ;
}
												

最新文章

  1. mybatis入门基础(六)----高级映射(一对一,一对多,多对多)
  2. WDCP突破phpmyadmin导入文件时只有20M
  3. 浏览器控制台console
  4. java在cmd下编译和执行引用jar的类
  5. VB 中Sub和Function的区别
  6. BZOJ4515: [Sdoi2016]游戏
  7. Oracle中用一条Sql实现任意的行转列拼接 多行拼接
  8. Linux下检测IP地址冲突及解决方法
  9. SpringMvc的数据绑定流程
  10. java eclise的配置
  11. Beaglebone Back学习四(GPIO实验)
  12. Ubuntu14.04搭建cocos2dx2.2.5开发环境(超级具体)
  13. hdu-1978_How many ways dfs+记忆化搜索
  14. c++动态库与静态库
  15. 交作业啊,python爬取58的页面
  16. Hystrix-request cache(请求缓存)
  17. Erlang cowboy 处理简单的HTTP请求
  18. nginx部署静态网站
  19. JavaScript的对象详解
  20. css+js杂记

热门文章

  1. BZOJ_day9
  2. PHP 5.4语法改进与弃用特性
  3. intellij IDEA与springboot项目建立
  4. Windows下查看某个端口被哪个服务占用
  5. Java之戳中痛点 - (2)取余用偶判断,不要用奇判断
  6. 【洛谷 P3834】 可持久化线段树1(主席树)
  7. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
  8. bzoj 1011 近似估计
  9. python3 进程_multiprocessing模块
  10. LeetCode 3 :Min Stack