UVa10075 - Airlines(所有点对之间的最短距离)

简介: UVa10075 - Airlines(所有点对之间的最短距离)
#include <cstdio>#include <map>#include <string>#include <cmath>#include <algorithm>usingnamespacestd;
constintN=110, INF=0x3f3f3f3f;
constintSTRLEN=25;
constdoublePI=acos(-1);
constintR=6378;
structLocation{
doublelati, longti;
};
Locationlocation[N];
intf[N][N];
intn, m, q;
map<string, int>strMap;
intt;
boolinput();
voidsolve();
intdis(inta, intb);
intmain()
{
#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif // ONLINE_JUDGEt=1;
while (input()) {
solve();
    }
return0;
}
boolinput()
{
strMap.clear();
scanf("%d%d%d", &n, &m, &q);
if (n==0&&m==0&&q==0) returnfalse;
charbuf[STRLEN];
for (inti=0; i<n; i++) {
scanf("%s%lf%lf", buf, &location[i].lati, &location[i].longti);
stringstr=buf;
strMap[str] =i;
    }
for (inti=0; i<n; i++) {
for (intj=0; j<n; j++) {
if (i==j) f[i][j] =0;
elsef[i][j] =INF;
        }
    }
for (inti=0; i<m; i++) {
scanf("%s", buf);
strings=buf;
intu=strMap[s];
scanf("%s", buf);
s=buf;
intv=strMap[s];
f[u][v] =dis(u, v);
    }
returntrue;
}
intdis(inta, intb)
{
doublealpha=location[a].lati/180*PI;
doublebelta=location[a].longti/180*PI;
doublez1=R*sin(alpha);
doublex1=R*cos(alpha) *cos(belta);
doubley1=R*cos(alpha) *sin(belta);
alpha=location[b].lati/180*PI;
belta=location[b].longti/180*PI;
doublez2=R*sin(alpha);
doublex2=R*cos(alpha) *cos(belta);
doubley2=R*cos(alpha) *sin(belta);
doubletheta=acos((x1*x2+y1*y2+z1*z2) /R/R);
return (int)round(R*theta);
}
voidsolve()
{
for (intk=0; k<n; k++) {
for (inti=0; i<n; i++) {
for (intj=0; j<n; j++) {
if (f[i][k] !=INF&&f[k][j] !=INF) {
f[i][j] =min(f[i][j], f[i][k] +f[k][j]);
                }
            }
        }
    }
if (t>1) printf("\n");
printf("Case #%d\n", t++);
for (inti=0; i<q; i++) {
charbuf1[STRLEN], buf2[STRLEN];
scanf("%s%s", buf1, buf2);
strings1=buf1, s2=buf2;
intu=strMap[s1], v=strMap[s2];
if (f[u][v] !=INF) {
printf("%d km\n", f[u][v]);
        } else {
printf("no route exists\n");
        }
    }
}
目录
相关文章
|
4月前
|
人工智能 BI
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
44 0
|
4月前
|
机器学习/深度学习
【每日一题Day181】LC1026节点与其祖先之间的最大差值 | 树形dp
【每日一题Day181】LC1026节点与其祖先之间的最大差值 | 树形dp
35 0
|
机器学习/深度学习 定位技术
【每日一题Day109】LC1210穿过迷宫的最少移动次数 | BFS+dp
思路:使用BFS搜索,队列中存放三元组[蛇尾的横轴坐标x,蛇尾的纵轴坐标y,蛇的状态],当蛇为水平时,状态为0;当蛇为竖直时,状态为1
116 1
【每日一题Day109】LC1210穿过迷宫的最少移动次数 | BFS+dp
|
存储
【每日一题Day106】LC1129 颜色交替的最短路径 | BFS
返回长度为 n 的数组 answer,其中 answer[X] 是从节点 0 到节点 X 的红色边和蓝色边交替出现的最短路径的长度。如果不存在这样的路径,那么 answer[x] = -1。
140 0
AcWing 周赛13 3813. 最大路径权值(拓扑排序 DAG上dp)
AcWing 周赛13 3813. 最大路径权值(拓扑排序 DAG上dp)
103 0
AcWing 616. 两点间的距离
AcWing 616. 两点间的距离
88 0
AcWing 616. 两点间的距离
AcWing 617. 距离
AcWing 617. 距离
72 0
AcWing 617. 距离
|
算法
[leetcode] 2055蜡烛之间的盘子 | 前缀和
在这里做出处理,让下标从1开始,询问的区间两端也+1 我们开两个数组: nearL 以及 nearR nearL[i] 表示:在i 位置离着最近的左边的 ∣符号是在哪一个下标 nearR[i] 表示:在i 位置离着最近的右边的 ∣符号是在哪一个下标 如果说 s[i] == '|' 那么说 nearL[i]=i 并且 nearR[i]=i 让左区间等于离着当前位置最近的右边的 | 让右区间等于离着当前位置最近的左边的 | 然后就可以直接开始 前缀和 了
93 0
[leetcode] 2055蜡烛之间的盘子 | 前缀和