辣鸡看着好多大佬都在写博客,然后就模仿一下大佬。。。。。。。。。。。
作为省赛前的模拟,这个题仅仅是读题就把我们难倒了,蓝瘦,读了好久才读出题意,然后当时只有40分钟左右,果断放弃去改J,虽然最后也没改出来(最后知道是我忘了注释想死) 蓝瘦蓝瘦。
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 tothese website.
You have a list of several proxy servers, some of them can beconnected directly but others can’t. But you can visit proxy servers throughother proxy server by a one-way connection.
As we all know, the lag of internet visit will decide our feelingsof the visit. You have a very smart proxy software which will find the leastlag 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 isthe all the lags in your whole connection. You want to minimize the lag ofvisit, which proxy server you will choose?
Input
Multiple testcases, the first line is an integer T (T <= 100), indicating the number oftest 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 targetwebsite.
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 thelag 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 theproxy server which can be connected directly from your computer.
If there are multiple choices, you should output the proxy server with theleast 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 theleast, output “0” (without quotes).
Sample 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
Sample Output
3-101
然后怎么处理呢?不难想到我们应该从n+1向前便利,但是这是个有向图,所以在输入的时候要注意将方向反过来,并且要记下和0相连的点。然后我用的是spfa,然后优化了一下下。
(原来我都是用的裸地spfa,然后无限tle,超哥让我用vector优化,无限懵逼,搞了好久 zzzzzzzz)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
int dis[1010],f[1010],k[1005][1005];
bool vis[1010];
int n,m;
struct bbq
{
int y;
int z;
};
vector<bbq>adj[1010];
void spfa(int start)
{
int now;
for(int i=0; i<n+5; i++)
dis[i]=inf;
dis[start]=0;
memset(vis,0,sizeof vis);
vis[start]=1;
queue<int> q;
q.push(start);
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=0;
for(int i=0; i<adj[now].size(); i++)
{
if(dis[adj[now][i].y]>dis[now]+adj[now][i].z)
{
dis[adj[now][i].y]=dis[now]+adj[now][i].z;
if(vis[adj[now][i].y]==0)
{
int t=adj[now][i].y;
q.push(t);
int c=q.front();
if(dis[t]<dis[c])
{
while(c!=t)
{
q.pop();
q.push(c);
c=q.front();
}
}
vis[t]=1;
}
}
}
}
}
int main()
{
int t;
// freopen("C:\\Users\\Administrator\\Desktop\\data.in", "r", stdin);
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d %d",&n,&m);
int mount=0;
memset(f,0,sizeof f);
memset(k,0,sizeof k);
for(int i=0; i<1010; i++)
adj[i].clear();
for(int i=0; i<m; i++)
{
int x;
bbq h;
scanf("%d %d %d",&h.y,&x,&h.z);
if(h.y==0)
{
f[mount++]=x;
k[x][0]=h.z;
}
adj[x].push_back(h);
}
int minx=inf,u=0;
spfa(n+1);
// printf("%d\n",dis[0]);
if(dis[0]>=inf)
printf("-1\n");
else
{
for(int i=0; i<mount; i++)
{
if(dis[f[i]]+k[f[i]][0]<minx)
{
minx=dis[f[i]]+k[f[i]][0];
u=i;
}
else if(dis[f[i]]+k[f[i]][0]==minx)
{
u=min(u,f[i]);
}
}
if(minx==k[f[u]][0])
printf("0\n");
else printf("%d\n",f[u]);
}
}
}
}