山东省第七届 ACM 省赛 Proxy (最短路)
来源:互联网 发布:在线网络测速器手机版 编辑:程序博客网 时间:2024/05/19 21:42
Problem Description
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?
Input
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.
Output
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).
Example Input
43 60 1 101 2 12 4 40 3 23 2 13 4 72 40 2 100 1 51 2 42 1 71 30 2 10 1 21 2 11 30 2 100 1 21 2 1
Example Output
3-101
题意
给你一些代理服务器之间的连接与延时,求出你的计算机(0)与目标服务器(n+1)之间延时最小的路径,以及路径中和你的计算机相连的代理服务器编号,如果存在多个选择的话输出最小的编号。
如果答案刚好是目标服务器的编号,输出 0
,无法到达则输出 -1
。
思路
只是找寻最短路径长度的话比较容易,但是题目中还要输出路径中第二个点的编号。
假如我们在找寻最短路的时候记录了路径,又会出现一个如何让第二个点编号最小的问题。
于是考虑反向建图
求 n+1
到 0
的最短路,然后记录与 0
邻接的点,最终判断这些点加上边的费用是否等于 dist[0]
即可。
AC 代码
#include<iostream>#include<algorithm>#include<cmath>#include<stdio.h>#include<string.h>#include<queue>#include<stack>using namespace std;#define maxx 22000#define INF 0x3f3f3f3ftypedef pair<int,int> P;int n,m;bool vis[maxx];int dist[maxx];priority_queue<P,vector<P>,greater<P> >va;struct node{ int v; int next; int value;} edge[maxx];int head[maxx],tot;void init(){ memset(head,-1,sizeof(head)); tot=0; while(!va.empty())va.pop();}void addedge(int u,int v,int s){ edge[tot].v=v; edge[tot].next=head[u]; edge[tot].value=s; head[u]=tot++;}void spfa(int s) // 求最短路径长度{ memset(vis,false,sizeof(vis)); memset(dist,INF,sizeof(dist)); vis[s]=true; dist[s]=0; queue<int>que; que.push(s); while(!que.empty()) { int u=que.front(); que.pop(); vis[u]=false; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(dist[v]>dist[u]+edge[i].value) { dist[v]=dist[u]+edge[i].value; if(!vis[v]) { vis[v]=true; que.push(v); } } } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); init(); for(int i=0; i<m; i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); addedge(y,x,z); if(x==0) // 存储与0邻接的点 va.push(P(y,z)); } spfa(n+1); if(dist[0]==INF) // 无法到达 printf("-1\n"); else { while(!va.empty()) { P p=va.top(); va.pop(); if(dist[p.first]+p.second==dist[0]) // 找到一个编号最小且在最短路上的点 { if(p.first==n+1) // 如果该点是 n+1 printf("0\n"); else printf("%d\n",p.first); break; } } } } return 0;}
- 山东省第七届 ACM 省赛 Proxy (最短路)
- 第七届山东省ACM省赛 C Proxy
- 第七届ACM山东省赛-C Proxy
- 第七届 山东省ACM Proxy(SPFA+反向建边)
- 山东省第七届ACM大学生程序设计竞赛 C:Proxy
- 山东省第一届ACM省赛 E SDUT 2155 Emergency(最短路)
- 山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
- 山东省第七届 ACM 省赛 Feed the monkey (dp)
- 山东省第七届ACM省赛------Julyed
- 山东省第七届ACM省赛------Fibonacci
- 山东省第七届ACM省赛------Triple Nim
- 山东省第七届ACM省赛------Reversed Words
- 山东省第七届ACM省赛------Memory Leak
- 山东省第七届ACM省赛 Triple Nim
- 第七届山东省ACM省赛 A Julyed
- 第七届山东省ACM省赛 B Fibonacci
- “浪潮杯”山东省第七届ACM大学生程序设计竞赛【C - Proxy】
- 山东省第七届ACM省赛------The Binding of Isaac
- ★★★★★背包问题——“01背包”"完全背包"详解及实现(包含背包中具体物品的求解)
- shell读取文件每一行的方式
- Linux下crontab命令的用法
- JavaScript学习笔记6-函数的定义和调用
- shiro和spring的集成(详细步骤)
- 山东省第七届 ACM 省赛 Proxy (最短路)
- Redis都能做什么?Redis 11种Web应用场景举例
- 【OpenCV3】图像通道分离与合并——cv::split()与cv::merge()详解
- [Android] 解决设置了Intent.FLAG_ACTIVITY_NO_ANIMATION 跳转activity还有动画的问题
- CentOS7.3下Docker自定义镜像
- 解决listview,gridview因为分页加载导致的数据复用问题
- Hibernate保存明细表时报 object references an unsaved transient instance错误
- 【单片机】I2C通信原理
- 制作KaliU盘系统