第七届 山东省ACM Proxy(SPFA+反向建边)

来源:互联网 发布:ppt图表无法修改数据 编辑:程序博客网 时间:2024/06/11 06:13

Proxy

Time Limit: 2000MS Memory Limit: 131072KB
Submit 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

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

Hint

Author

 “浪潮杯”山东省第七届ACM大学生程序设计竞赛

题意:给你代理服务器之间连接和延时,求出你的服务器(0)和目标服务器(n+1)延时最少的路径和你的服务器之间相连的服务器的编号,如果有多个输出编号最小的,如果为目标服务器,输出0,如果没有路径输出-1.

题解:最短路是确定了,但是由于需要标号是最小的,所以可以反向建边,这样的话在终点是0的时候记录下当前的最后节点就可以了。


当时比赛的时候直接用一个属性值来标记了,所以在每个节点上都记录了当前节点是来自于从哪个节点中出来的,有些麻烦,并且当时这个题是我敲的,一不小心敲成了双向图了,我也是服,所以WR了两次,如果不是这样耽误了时间导致后面没法敲可能就不会打铜了。。。

/*单源最短路SPFA时间复杂度O(kE)这个是队列实现,有时候改成栈实现会更加快,很容易修改这个复杂度是不定的*/#include<iostream>#include<stdio.h>#include<vector>#include<string.h>#include<queue>using namespace std;const int MAXN=1010;const int INF=0x3f3f3f3f;struct Edge{    int v;    int cost;    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}};vector <Edge> E[MAXN];void addedge(int u,int v,int w){    E[u].push_back( Edge(v,w) );}bool vis[MAXN];///在队列标志int cnt[MAXN];///每个点的入队列次数  解决负权值int dist[MAXN];int res ;bool SPFA(int start,int n){    memset(vis,false,sizeof(vis));    for(int i=0;i<=n;i++)        dist[i]=INF;    memset(cnt,0,sizeof(cnt));    vis[start]=true;    dist[start]=0;    queue<int>que;    while(!que.empty())  que.pop();    que.push(start);    cnt[start]=1;    while(!que.empty()){        int u=que.front();    //cout<<u<<"-----------"<<endl;        que.pop();        vis[u]=false;        for(int i=0;i<E[u].size();i++){            int v=E[u][i].v;            int dis = dist[u]+E[u][i].cost;            if( v==0){  ///如果下一个节点是到达0的,判断后更新              if(dist[v]>dis){                if(u==n) res = 0;///注意样例中第三组,当出现直接从0到目的节点的这种只能特殊处理下                else res = u;              }              else if((dist[v]==dis&&res>u))                res = u;            }            if(dist[v]>dist[u]+E[u][i].cost){                dist[v]=dist[u]+E[u][i].cost;                if(!vis[v]){                    vis[v]=true;                    que.push(v);                    if(++cnt[v]>n)return false;///cnt[i] 为入队列次数,用来判定是否存在负环回路                }            }        }    }    return true;}int main(){    int a,b,time;    int T,N,M;    scanf("%d",&T);    while(T--){        scanf("%d%d",&N,&M);        for(int i=0;i<=N+1;++i){            E[i].clear();        }        res = -1;        for(int i=0;i<M;i++){            scanf("%d%d%d",&a,&b,&time);            addedge(b,a,time);        }        SPFA(N+1,N+1);///MAXN-1        printf("%d\n",res);    }    return 0;}






0 0
原创粉丝点击