最短路径(比较)

https://nanti.jisuanke.com/t/41349

This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are VVV (Numbers 111 to VVV) fire-fighting points in ACM city. These fire-fighting points have EEE roads to communicate with each other. Among them, there is a fire-fighting hero in the SSS fire-fighting point, and the fire-fighting team is distributed in K fire-fighting points. If a fire-fighting point needs to be put out, the fire-fighting hero or the fire-fighting team must arrive as soon as possible, that is, to choose the shortest route to arrive.

Today, our fire-fighting heroes want to challenge the fire-fighting team. The challenge is to: The maximum value of the shortest path for a fire-fighting hero to go to others fire-fighting points is compared with the maximum value of the shortest path for a fire-fighting team to go to others fire-fighting points from any point in their fire-fighting points. Because firefighting heroes are different and run faster, the maximum value of the shortest path they get should be discounted first, that is, multiplied by a coefficient of 1C\frac{1}{C}C1, and then compared. The smaller one wins. Who is the real firefighter in this situation?

Who is the real firefighter in this situation?

Input

The first line contains a positive integer T(1≤T≤10)T (1\le T \le 10)T(1T10), which indicates that there are TTT cases of test data.

The format of each case of test data is as follows:

  • Line 111 contains five positive integers V(1≤V≤1000)V (1 \le V \le 1000)V(1V1000), E(V−1≤E≤V∗V2)E (V-1 \le E \le \frac{V*V}{2})E(V1E2VV), S(1≤S≤V)S (1 \le S \le V)S(1SV), K(1≤K≤V)K (1\le K \le V)K(1KV) and C(1≤C≤10)C (1\le C\le 10)C(1C10), the meanings are shown above.
  • Line 222 contains KKK positive integers, which in turn denotes the location number of the fire-fighting point where the fire-fighting team is located.

In the next EEE line, three positive integers i,j(1≤i,j≤V)i, j (1 \le i, j \le V)i,j(1i,jV) and L(1≤L≤10000)L (1 \le L \le 10000)L(1L10000) per line. Represents a path, i,ji, ji,j as the endpoint (fire-fighting point), LLL as the length of the path.

Output

Each case of test data outputs one line, which is a integer. That is, the maximum value of the shortest path of the winner (If the fire hero wins, the maximum value before the discount should be output). A draw is also a victory for fire-fighting hero.

样例输入

1
4 7 3 2 2
1 4
1 2 7
1 3 2
1 4 6 
2 1 1
2 4 1
3 2 1
3 4 3

样例输出

2
题意:救火英雄和救援对之间的比较。。
救火英雄去往每一个城市(救火点)的最短路径的最大值乘于1/c
救援队(注意救援队是一个整体)前往每一个城市(救火点)的最短路径的最大值
两个值比较输出较短的那一个。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 998244353
#define PI acos(-1)
using namespace std;
typedef long long ll ;

int ma[1020][1020];
int v , e , s , k , c ;
int dis[1020];
int vis[1020];
int a[1020];

void Dijia(ll r)
{
    for(int i = 1 ; i <= v ; i++)
    {
        vis[i] = 0 ;
        dis[i] = ma[r][i];
    }
    vis[r] = 1 ;
    for(int i = 1 ; i < v ; i++)
    {
        int min1 = INF;
        int pos ;
        for(int j = 1 ; j <= v ; j++)
        {
            if(!vis[j] && min1 > dis[j])
            {
                min1 = dis[j];
                pos = j ;
            }
        }
        vis[pos] = 1 ;
        for(int j = 1 ; j <= v ; j++)
        {
            dis[j] = min(dis[j] , dis[pos] + ma[pos][j]);
        }
    }
}

int main()
{
    int t ;
    scanf("%d" , &t);
    while(t--)
    {
        int u , vv ,  w ;
        int h = - INF , cc = -INF;

        scanf("%d%d%d%d%d" , &v , &e , &s ,&k , &c);

        for(int i = 1 ; i <= v ; i++)
        {
            for(int j = 1 ; j <= v ; j++)
            {
                if(i == j) ma[i][j] = 0 ;
                else ma[i][j] = INF ;
            }
        }


        for(int i = 0 ; i < k ; i++)
        {
            scanf("%d" , &a[i]);
        }



        for(int i = 0 ; i < e ; i++)
        {
            scanf("%d%d%d" , &u , &vv , &w);
            ma[u][vv] = ma[vv][u] = min(ma[u][vv] , w);
        }

        Dijia(s);

        for(int i = 1 ; i <= v ; i++)
        {
            
            h = max(h , dis[i]);
        }
        
        for(int i = 0 ; i < k ; i++)
        {
            for(int j = i + 1 ; j < k ; j++)
            {
                ma[a[i]][a[j]] = ma[a[j]][a[i]] = 0 ;
            }
        }
        Dijia(a[0]);
        for(int i = 1 ; i <= v ; i++)
        {
            
            cc = max(cc , dis[i]);
        }
        

        if(h <= cc * c)
        {
            printf("%d\n" , h);
        }
        else
            printf("%d\n" , cc);
    }


    return 0 ;
}

第二种方法是增加一个点,将与每一个救援对之间的距离赋值为0。该点位源点;

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 998244353
#define PI acos(-1)
using namespace std;
typedef long long ll ;
ll v , e , s , k , c ;
ll ma[1020][1020];
ll vis[1020];
ll dis[1020];


void Dijia(ll r)
{
    for(int i = 1 ; i <= v ; i++)
    {
        vis[i] = 0 ;
        dis[i] = ma[r][i] ;
    }
    vis[r] = 1 ;

    for(int i = 1 ; i < v ; i++)
    {

        ll min1 = INF ;
        ll pos ;

        for(int j = 1 ; j <= v ; j++)
        {
            if(!vis[j] && min1 > dis[j])
            {
                min1 = dis[j];
                pos = j ;
            }
        }
        vis[pos] = 1 ;
        for(int j = 1 ; j <= v ; j++)
        {
            dis[j] = min(dis[j] , dis[pos] + ma[pos][j]);
        }
    }
}


int main()
{
    int t ;
    scanf("%d" , &t);
    while(t--)
    {
        ll h = -INF , cc = -INF ;
        scanf("%lld%lld%lld%lld%lld" , &v , &e , &s , &k , &c);
        ll tea ;
        for(int i = 1 ; i <= v+1 ; i++)
        {
            for(int j = 1 ; j <= v+1 ; j++)
            {
                if(i == j) ma[i][j] = 0 ;
                else ma[i][j] = INF;
            }
        }

        for(int i = 0 ; i < k ; i++)
        {
            scanf("%lld" , &tea);
            ma[v+1][tea] = ma[tea][v+1] = 0 ;
        }

        for(int i = 0 ; i < e ; i++)
        {
            ll f , to , w ;
            scanf("%lld%lld%lld" , &f , &to , &w);
            ma[f][to] = ma[to][f] = min(ma[f][to] , w);
        }

        Dijia(s);

        for(int i = 1 ; i <= v ; i++)
        {
            h = max(dis[i] , h);
        }

        Dijia(v+1);
        for(int i = 1 ; i <= v ; i++)
        {
            cc = max(dis[i] , cc);
        }
        if(h <= cc * c)
        {
            printf("%lld\n" , h);
        }
        else
        {
            printf("%lld\n" , cc);
        }
    }


    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/11489149.html