Proxy 最短路 反向建图
来源:互联网 发布:海地软件 编辑:程序博客网 时间:2024/06/13 22:36
感觉自己越来越菜了。
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).
题意:找到最短路上 和0相连的最小服务器
如果0和n+1直接相连输出 0;
如果无法达到n+1直接输出-1;
否则输出和0相连的最小服务器
反间建图,判断边界 如果0的前一个节点是n+1,证明直接相连。
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 101010;const int inf = 1e9+7; int pre[N];struct node{ int v, w; bool operator <(const node &rhs) const { return w>rhs.w; }};struct Edge{ int to, w; int nex;}edge[101010];int tot, head[N], d[N], vis[N];void addedge(int u,int v,int w){ edge[tot].to = v; edge[tot].w = w; edge[tot].nex = head[u]; head[u] = tot++;}int n,m;void init(){ tot=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(d,0,sizeof(d));}void dijkstra(int s){ for(int i = 0; i <= n+1; i++) d[i] = inf; d[s] = 0; priority_queue <node> Q; node a,b; a.v = s, a.w = 0; Q.push(a); while(!Q.empty()) { node top = Q.top(); Q.pop(); int u = top.v; if(vis[u]) continue; vis[u]=1; for(int i = head[u]; i!=-1; i = edge[i].nex) { int v = edge[i].to; if(d[v] >d[u] + edge[i].w) { pre[v]=u; d[v] = d[u] + edge[i].w; b.v = v,b.w = d[v]; Q.push(b); } else if(d[v]==d[u]+edge[i].w) { if(u<pre[v]) pre[v]=u; } } }}int main(){ int t; scanf("%d",&t); while(t--) { init(); scanf("%d%d",&n,&m); int f=0; int ss=0; for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); addedge(b,a,c); } dijkstra(n+1); if(d[0]>=inf) printf("-1\n"); else if(pre[0]==n+1) printf("0\n"); else printf("%d\n",pre[0]); }}
- Proxy 最短路 反向建图
- poj1511 spfa最短路+手写邻接表+反向建图
- POJ Silver Cow Party (最短路+反向建图)
- cf#33-B - String Problem-反向建图+最短路
- poj1062昂贵的聘礼【最短路反向建图】
- Choose the best route 【最短路】+【反向建图】
- hdu 1535 Invitation Cards(有向图的来回最短路,要反向建图)
- Poj 3268 Silver Cow Party + Poj 1511 Invitation Cards (最短路反向建图)
- HDOJ Choose the best route 2680【最短路Dijkstra+反向建图】
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- 杭电2680-Choose the best route(最短路反向建图)
- poj--3268--Silver Cow Party(最短路)(建反向图)
- hdu 1535 Invitation Cards spfa求最短路,反向建图再来一次
- codevs 1992 聚会 最短路+SPFA优化+反向建边
- SDUT 3562 Proxy【思维+最短路SPFA】
- HDU 1535 Invitation Cards 有向图的来回最短路(邻接表反向建图)+spfa
- HDU 2680 Choose the best route (最短路+反向图)
- HDOJ 2680 Choose the best route(最短路—反向图,dijkstra算法)
- java对象在set属性时为什么不更新数据库
- 游戏人生----序章
- 基于Three.js的360X180度全景图预览插件
- 面试题------数值整数次方
- HTTP与HTTPS的区别
- Proxy 最短路 反向建图
- There is already '******' bean method
- jQuery 选择器 _ 菜鸟教程
- Windows Shell 编程学习总结
- Android Studio 真机调试注意
- SOAPUI作为测试端测试接口调用
- 图像滤波基础一
- windows使用nginx实现网站负载均衡测试实例
- 求100到200间的素数