GYM 100827 L.Wormhole(Floyd)
2017-03-10 14:47
363 查看
Description
给出n个星球的名称和三维坐标,距离定义为欧氏距离,其中有m对星球之间存在虫洞,那么这对星球之间距离为0,q次查询,每次查询一对星球之间的最短距离
Input
第一行一整数T表示用例组数,每组用例首先输入一整数n表示星球数量,之后输入每个星球的名字和三维坐标x,y,x,然后是一整数m表示虫洞数量,之后m行每行输入两个星球的名字表示这对星球之间存在虫洞,然后是一整数q表示查询数,之后q行每行两个星球的名字表示查询这两个星球的最短距离(1<=T<=10,1<=n<=60,0<=x,y,z<=2e6,0<=m<=40,1<=q<=20,名字不超过50个字符)
Output
对于每次查询,输出这两个星球之间最短距离
Sample 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
Sample 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.
Solution
才60个点,建好图Floyd跑一遍即可
Code
给出n个星球的名称和三维坐标,距离定义为欧氏距离,其中有m对星球之间存在虫洞,那么这对星球之间距离为0,q次查询,每次查询一对星球之间的最短距离
Input
第一行一整数T表示用例组数,每组用例首先输入一整数n表示星球数量,之后输入每个星球的名字和三维坐标x,y,x,然后是一整数m表示虫洞数量,之后m行每行输入两个星球的名字表示这对星球之间存在虫洞,然后是一整数q表示查询数,之后q行每行两个星球的名字表示查询这两个星球的最短距离(1<=T<=10,1<=n<=60,0<=x,y,z<=2e6,0<=m<=40,1<=q<=20,名字不超过50个字符)
Output
对于每次查询,输出这两个星球之间最短距离
Sample 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
Sample 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.
Solution
才60个点,建好图Floyd跑一遍即可
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define maxn 66 map<string,int>M; int T,n,m,q,x[maxn],y[maxn],z[maxn]; double dis[maxn][maxn]; string s[maxn]; double get(int i,int j) { return sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)+pow(z[i]-z[j],2)); } int main() { scanf("%d",&T); for(int Case=1;Case<=T;Case++) { M.clear(); scanf("%d",&n); for(int i=1;i<=n;i++) { cin>>s[i]>>x[i]>>y[i]>>z[i]; M[s[i]]=i; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=get(i,j); scanf("%d",&m); while(m--) { string a,b; cin>>a>>b; dis[M[a]][M[b]]=0; } for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); scanf("%d",&q); printf("Case %d:\n",Case); while(q--) { string a,b; char c[maxn],d[maxn]; scanf("%s%s",c,d); a=c,b=d; printf("The distance from %s to %s is %.0f parsecs.\n",c,d,dis[M[a]][M[b]]); } } return 0; }
相关文章推荐
- GYM 100883 I.Teleportia(Floyd)
- GYM 100827 G.Number Game(博弈论)
- GYM 100827 H.Pushups(01背包)
- GYM 101147 B.Street(Floyd)
- GYM 100827 I.Salary Inequity(线段树)
- GYM 100827 A.Runes(水~)
- GYM 100827 K.Towers(dfs)
- GYM 100827 D.Function(高斯消元)
- GYM 100827 E.Hill Number(数位DP)
- Gym 101572 I.Import Spaghetti【Floyd最小环+输出路径】
- [CF Gym 100827C] Containment [2014-2015 ACM-ICPC Pacific Northwest Regional Contest C]
- GYM 100827 F.Knights(dp+矩阵快速幂)
- [CF Gym 100827E] Hill Number [2014-2015 ACM-ICPC Pacific Northwest Regional Contest E]
- GYM 100090 C.Graph Restoration(Floyd)
- SYSU-7,Gym 100273J, Japanese Writing,hash
- Codeforces gym 100971 D. Laying Cables 单调栈
- CodeForces Gym 100989F Mission in Amman (A)
- 求无向图最小环算法-floyd
- Gym 100646 Problem C: LCR 模拟题
- hdu 1869 六度分离(floyd)