Proxy
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
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
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
题目大意:
题目大意:
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
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
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
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