The shortest path (Comparative)

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 11 1 contains five positive integers V (1≤V≤1000) V (1 \ and V \ 1000) V ( 1 V 1 0 0 0 ), E (V-1≤E≤V * V2) E (V-1 \ and E \ and \ frac {V} * {V} 2) E ( V - 1 E 2 V * V ), S (1≤S≤V) S (1 \ and S \ and V) S ( 1 S V ), K (1≤K≤V) K (1 \ and K \ and V) K ( 1 K V ) andC(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.

Sample input

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

Sample Output

2 
meaning of the questions: fire a comparison between the hero and rescue right. .
Destined for the shortest maximum as the light goes out every city (fire point) path multiplied by 1 / c
maximum Rescue Team (note rescue team as a whole) go the shortest of each city (fire point) the path of
the two values compare output short one.
//#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 ;
}

The second method is to add a point, it will be assigned to 0 and the distance between each of the rescue right. The point source;

//#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 ;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11489149.html