山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
来源:互联网 发布:软件开发收费 编辑:程序博客网 时间:2024/06/06 14:05
题目描述
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
真正的黑历史,省赛的时候知道题意就是没思路,要是早点多做几道邝斌的题就过了。就暑假这段时间练了下,结果就1A。。。我还是太菜
给一个有向图,求0点到n+1点的最短路径上直接与0点连接的点,如果有多条最短路就输出编号最小的那个。思路就是反向建图,求出终点到起点的最短路径,然后从小到大枚举所有与0点连接的点n,只要dis[n]+map[n][0]=dis[0]成立就退出,注意起点直接与终点相连的情况。
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 200010#define Mod 10001using namespace std;int map[1005][1005],cost[1005][1005],vis[1005],dis[1005],n,m;int p[MAXN],level[MAXN];void dijkstra(int s){ int i,j,min,v; memset(vis,0,sizeof(vis)); for(i=0; i<=n+1; ++i) { dis[i]=map[s][i]; } dis[s]=0; vis[s]=1; for(i=0; i<=n+1; ++i) { v=-1; min=INF; for(j=0; j<=n+1; ++j) { if(!vis[j]&&dis[j]<min) { min=dis[j]; v=j; } } if(v==-1) break; vis[v]=1; for(j=0; j<=n+1; ++j) { if(!vis[j]&&map[v][j]<INF) { if(dis[j]>dis[v]+map[v][j]) { dis[j]=dis[v]+map[v][j]; } } } }}int near[MAXN];int main(){ int t,u,v,w; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0; i<=n+1; i++) for(int j=0; j<=n+1; ++j) map[i][j]=INF; int ne=0; while(m--) { scanf("%d%d%d",&u,&v,&w); map[v][u]=w; if(u==0) near[ne++]=v; } dijkstra(n+1); if(dis[0]==INF) { printf("-1\n"); continue; } sort(near,near+ne); int ans; for(int i=0; i<ne; ++i) { int u=near[i]; if(dis[u]+map[u][0]==dis[0]) { if(u==n+1) { ans=0; break; } ans=u; break; } } printf("%d\n",ans); } return 0;}
- 山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
- 山东省第七届 ACM 省赛 Proxy (最短路)
- 山东省第四届ACM省赛题——Rescue The Princess(计算几何)
- 山东省第四届ACM省赛题——Contest Print Server(模拟)
- 山东省第四届ACM省赛题——Square Number(平方数的性质)
- 山东省第四届ACM省赛题——Alice and Bob(二进制)
- 山东省第四届acm省赛
- 山东省第一届ACM省赛题——Balloons(搜索)
- 山东省第七届ACM省赛题——Fibonacci(二分)
- 第七届ACM山东省赛-C Proxy
- 第七届山东省ACM省赛 C Proxy
- 第四届 山东省ACM Rescue The Princess(计算几何)
- Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
- 一场刺激的游戏——很文艺的山东省第四届ACM赛总结(菜鸟版)
- [2013山东省第四届ACM大学生程序设计竞赛]——Contest Print Server
- [2013山东省第四届ACM大学生程序设计竞赛]——Rescue The Princess
- [2013山东省第四届ACM大学生程序设计竞赛]——Alice and Bob
- 2013年山东省第四届ACM大学生程序设计竞赛——A^X mod P
- 两种丢失更新的区别
- 实现点击当前输入框以外的地方,自动隐藏输入法软键盘
- spring-boot研究二:spring-boot单元测试
- JCL 挂掉原因
- 数据库基础教程(2):范式
- 山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
- hdoj 1069 Monkey and Banana(上升子序列最大和)
- springmvc运行后是一个空白页面,后台不报错
- Android中的两种坐标系
- SqlDataAdapter简单介绍
- Southern Subregion Problem I. Plugs and Sockets
- Apache Spark探秘:实现Map-side Join和Reduce-side Join
- 安卓学习笔记---最全面的使用idea和gradle把java项目打包成jar的方法
- A Design Framework for Highly Concurrent Systems (待翻译)