SDUT 3562 Proxy【思维+最短路SPFA】
2017-04-27 21:35
281 查看
Proxy
Time Limit: 2000MS Memory Limit: 131072KB[align=center]Submit Statistic Discuss[/align]
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
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
Example Output
3
-1
0
1
Hint
Author
“浪潮杯”山东省第七届ACM大学生程序设计竞赛题目大意:
题目大意:
M条有向边。
现在有0~N+1些个点,我们的电脑是0号,目标网站是n+1号,我们可以选择和0号电脑直接相连的某个点作为连接点,使得这个点跑到N+1这个点的最短路最小。
如果无论选哪个点都不能跑到n+1这个点上来,输出-1.
如果0和n+1直接相连的某条边就是从0到n+1的最短路,输出0.
否则输出一个最小编号,使得满足题意。
思路:
如果我们将和0相连的所有边作为起点,跑单源最短路的话很显然要超时的,那么我们将N+1作为源点,然后跑单源最短路,反向建图,那么就能得到dist【i】.表示点i到点n的最短路。
接下来我们按照题意模拟:
①如果dist【0】==inf,输出-1.
②如果dist【0】==从0到n+1直接相连的一条边的权值.那么输出0.
③如果不满足①也不满足②,那么我们将和0直接相连的那些点都拿出来(u),首先维护最小的dist【i】+W(0,u);如果值有相同的情况出现,那么对应我们找到最小的那个编号即可。
Ac代码:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int from,to,w,next;
}e[200000*4];
typedef long long int LL ;
const int N = 2000+7;
const int MOD = 1e9+7;
#define abs(x) (((x)>0)?(x):-(x))
/***************************************/
int n,m;
int head[150000];
int dist[150000];
int vis[150000];
int cont;
void add(int from,int to,int w)
{
e[cont].from=from;
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA(int ss)
{
queue<int >s;
memset(vis,0,sizeof(vis));
for(int i=0;i<=n+1;i++)dist[i]=0x3f3f3f3f;
dist[ss]=0;
s.push(ss);
while(!s.empty())
{
int u=s.front();s.pop();vis[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(dist[v]>dist[u]+w)
{
dist[v]=dist[u]+w;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
}
int main(){
int _;
scanf("%d",&_);
while(_--){
cont=0;
scanf("%d%d",&n,&m);
int minn=0x3f3f3f3f;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
if(x==0&&y==n+1)minn=min(w,minn);
add(y,x,w);
}
SPFA(n+1);
if(dist[0]==0x3f3f3f3f)printf("-1\n");
else
{
if(minn==dist[0])printf("0\n");
else
{
int maxn=0x3f3f3f3f;
int ans=-1;
for(int i=0;i<cont;i++)
{
if(e[i].to==0)
{
int u=e[i].from;
int w=e[i].w;
if(dist[u]==0x3f3f3f3f)continue;
if(dist[u]+w<maxn)
{
maxn=dist[u]+w;
ans=u;
}
else if(dist[u]+w==maxn)
{
ans=min(ans,u);
}
}
}
printf("%d\n",ans);
}
}
}
return 0;
}
Proxy
Time Limit: 2000MS Memory Limit: 131072KBSubmit Statistic Discuss
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
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
Example Output
3 -1 0 1
Hint
Author
“浪潮杯”山东省第七届ACM大学生程序设计竞赛
相关文章推荐
- sdut3562-求字典序最小的最短路 按顶点排序后spfa的反例
- SDUT2622 最短路(spfa)
- (模板题)sdut 2143 图结构练习——最短路径(SPFA求最短路)
- SDUT2493 Constructing Roads(最短路,spfa)
- Proxy(2016山东省省赛C)(最短路)(spfa)
- HDU1217 - Arbitrage - 思维+乘法求最大的最短路(Floyd+spfa)
- SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
- SDUT 2894 最短路(SPFA or Bleman)
- SDUT 2894-C(最短路spfa)
- Floyd,Dijkstra,SPFA模板整理(以[HDU2544-最短路]为例 )
- 最短路之SPFA模板
- poj 3259 Wormholes 【spfa判负环---求最短路】
- 最短路spfa
- 【最短路】【spfa】CDOJ1633 去年春恨却来时,落花人独立,微雨燕双飞
- 最短路模板 floyd+dijkstra+spfa
- Codeforces 667D World Tour【最短路+思维】好题!
- 蓝桥杯 最短路(spfa(优先队列+迪杰斯特拉))
- hdu 2377 Bus Pass(最短路spfa)
- 蓝桥杯 - 算法训练 - ALGO - 5 最短路(spfa)
- poj 3159 Candies(查分约束+堆栈优化的spfa最短路模板)