当前位置: 首页 > 工具软件 > Wormhole > 使用案例 >

K - Wormhole Gym - 101653W(最短路)

鄢翰藻
2023-12-01

K - Wormhole Gym - 101653W(最短路)

Wormhole
With our time on Earth coming to an end, Cooper and Amelia have volunteered to undertake what could be the most important mission in human history: travelling beyond this galaxy
to discover whether mankind has a future among the stars. Fortunately, astronomers have identified several potentially inhabitable planets and have also discovered that some of these planets
have wormholes joining them, which effectively makes the travel distance between these wormhole
connected planets zero. For all other planets, the travel distance between them is simply the Euclidean distance between the planets. Given the location of Earth, planets, and wormholes, find
the shortest travel distance between any pairs of planets.
Input
• The first line of input is a single integer, T (1 ≤ T ≤ 10) the number of test cases.
• Each test case consists of planets, wormholes, and a set of distance queries.
• The planets list for a test case starts with a single integer, p (1 ≤ p ≤ 60), the number of
planets. Following this are p lines, where each line contains a planet name along with the planet’s integer coordinates, name x y z (0 ≤ x, y, x ≤ 2 · 106
) The names of the planets will consist only of ASCII letters and numbers, and will always start with an ASCII letter.
Planet names are case-sensitive (Earth and earth are distinct planets). The length of a planet name will never be greater than 50 characters. All coordinates are given in parsecs.
• The wormholes list for a test case starts with a single integer, w (0 ≤ w ≤ 40), the number of wormholes, followed by the list of w wormholes. Each wormhole consists of two planet names separated by a space. The first planet name marks the entrance of wormhole, and the second planet name marks the exit from the wormhole. The planets that mark wormholes will be chosen from the list of planets given in the preceding section. Note: you can’t enter a wormhole at its exit.
• The queries list for a test case starts with a single integer, q (1 ≤ q ≤ 20), the number of queries. Each query consists of two planet names separated by a space. Both planets will have been listed in the planet list.

Output
For each test case, output a line, “Case i:”, the number of the ith test case. Then, for each query in that test case, output a line that states “The distance from planet1 to planet2 is d parsecs.”, where the planets are the names from the query and d is the shortest possible travel distance between the two planets. Round d to the nearest integer.

Sample Input
input:
3
4
Earth 0 0 0
Proxima 5 0 0
Barnards 5 5 0
Sirius 0 5 0
2
Earth Barnards
Barnards Sirius
6
Earth Proxima
Earth Barnards
Earth Sirius
Proxima Earth
Barnards Earth
Sirius Earth
3
z1 0 0 0
z2 10 10 10
z3 10 0 0
1
z1 z2
3
z2 z1
z1 z2
z1 z3
2
Mars 12345 98765 87654
Jupiter 45678 65432 11111
0
1
Mars Jupiter
output:
Case 1:
The distance from Earth to Proxima is 5 parsecs.
The distance from Earth to Barnards is 0 parsecs.
The distance from Earth to Sirius is 0 parsecs.
The distance from Proxima to Earth is 5 parsecs.
The distance from Barnards to Earth is 5 parsecs.
The distance from Sirius to Earth is 5 parsecs.
Case 2:
The distance from z2 to z1 is 17 parsecs.
The distance from z1 to z2 is 0 parsecs.
The distance from z1 to z3 is 10 parsecs.
Case 3:
The distance from Mars to Jupiter is 89894 parsecs.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f
int q, w, p;
map<string, int>mp;

double dis[105][105];
string a[105];

struct node
{
    string s;
    double x, y, z;
} str[105];

void floyd()
{
    for(int k = 1; k <= p; k ++)
    {
        for(int i = 1; i <=p; i ++)
        {
            for(int j = 1; j <= p; j++)
            {
                if(dis[i][j] > dis[i][k] + dis[k][j])
                    dis[i][j] = dis[i][k] + dis[k][j];
            }
        }
    }
}



int main()
{
    int t;
    cin >> t;
    for(int z = 1; z<=t; z++)
    {
        memset(dis, INF, sizeof(dis));
        mp.clear();
        cin >> p;
        for(int i = 1; i <= p; i ++)
        {
            cin >> str[i].s >> str[i].x >> str[i].y >> str[i].z;
            mp[str[i].s] = i;
        }
        for(int i = 1; i <= p; i ++)
        {
            for(int j = i+1; j <=p; j++)
            {
                double num = (str[i].x-str[j].x)*(str[i].x-str[j].x)+(str[i].y-str[j].y)*(str[i].y-str[j].y)+(str[i].z-str[j].z)*(str[i].z-str[j].z);
                dis[i][j] = dis[j][i] = sqrt(num*1.0);
            }
        }
        cin >> w;
        while(w--)
        {
            string s1, s2;
            cin >> s1 >> s2;
            dis[mp[s1]][mp[s2]] = 0.0;
        }
        floyd();
        printf("Case %d:\n", z);
        cin >> q;
        while(q--)
        {
            string s1, s2;
            cin >> s1 >> s2;
            int tot = mp[s1];
            int ans = mp[s2];
            cout << "The distance from "<< s1 << " to " << s2 << " is " << (int)(dis[tot][ans]+0.5)<< " parsecs." << endl;
        }
    }
    return 0;
}

 类似资料: