第七届山东省ACM省赛 C 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
    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).




这是一道最短路问题。 要求有以下几点: ①求最短路,如果没有输出-1;

                                                                          ②输出最短路当中的距离起点最近的那个点

                                                                         ③存在多个最短路时,输出最小的那个点


第一点不用多少,套模版即可。

第二点,我们记录路径往往最容易查询距离终点最近的那个点。所以我们可以建反向边,这样就可以查询距离起点最近得了。

第三点,这是这道题的核心考点。  如果某点加上当前的路径长度刚好等于最短路的长度,那么说明存在两条最短路了,这时候比较当前点和原本终点的前一个点的大小,选择小的替换。



int INF = 1<<29;
int mp[1010][1010];
int n,m;
    int d[1010];
    int path[1010];
    bool vis[1010];
void init(int n)
{
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
    {
        if(i==j)
            mp[i][j]=0;
        else
            mp[i][j]=INF;
    }
}


void dijkstra(){
    int i,j,minn,v;


    for(i=0;i<n;i++){
        vis[i]=0;
        d[i]=mp[n-1][i];
        if(mp[n-1][i]<INF)
            path[i]=0;
        else
            path[i]=-1;
    }
    path[n-1]=0;
    for(i=0;i<n;i++)
    {
        minn=INF;
        for(j=0;j<n;j++)
            if(!vis[j] && d[j]<minn)
            {
                v=j;
                minn=d[j];
            }
        vis[v]=1;
        for(j=0;j<n;j++)
            if(!vis[j] && d[j]>mp[v][j]+d[v])
            {
                 d[j]=mp[v][j]+d[v];
                  path[j]=v;
            }
            else
            {
                  if(!vis[j]&&d[j]==mp[v][j]+d[v])
                  {
                      path[j]=min(path[j],v);
                  }
            }
    }
}
int main()
{
    int t,a,b,c;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        n+=2;
        init(n);
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            mp[b][a]=c;
        }
        dijkstra();
        if(d[0]==INF)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            cout<<path[0]<<endl;
        }


    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值