Problem 1037: Wormhole

Time Limits:  5000 MS   Memory Limits:  200000 KB

64-bit interger IO format:  %lld   Java class name:  Main

Description

With our time on Earth coming to an end, Cooper and Amelia have volunteered to under- take 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 iden- tified 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 Eu- clidean 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,
    i.e. 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


Output for Sample Input

   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.

Hint

   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

懒人用map写邻接表,果然够麻烦的,神奇的是写完可以直接编译,居然没报错,懂套路就是一SPFA水题。只是点从int换成了string。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
map<string,vector<pair<string,double> > >E;
map<string,double>d;
map<string,double>::iterator mit;
struct info
{
string s;
double x,y,z;
};
double dx(const info &a,const info &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
void spfa(const string &s,const string &t)
{
d[s]=0;
priority_queue<pair<double,string> >Q;
vector<pair<string,double> >::iterator it;
Q.push(pair<double,string>(-d[s],s));
while (!Q.empty())
{
string now=Q.top().second;
Q.pop();
for (it=E[now].begin(); it!=E[now].end(); it++)
{
string v=it->first;
if(d[v]>d[now]+it->second)
{
d[v]=d[now]+it->second;
Q.push(pair<double,string>(-d[v],v));
}
}
}
return ;
}
int main(void)
{
int tcase,i,j;
info pla[70];
cin>>tcase;
map<string,vector<pair<string,double> > >::iterator it;
for (int ca=1; ca<=tcase; ca++)
{
E.clear();
d.clear();
int p,q;
string s;
double x,y,z;
cin>>p;
for (i=0; i<p; i++)
{
cin>>pla[i].s>>pla[i].x>>pla[i].y>>pla[i].z;
}
for (i=0; i<p; i++)
{
d[pla[i].s]=1e9;
for (j=i+1; j<p; j++)
{
E[pla[i].s].push_back(pair<string,double>(pla[j].s,dx(pla[i],pla[j])));
E[pla[j].s].push_back(pair<string,double>(pla[i].s,dx(pla[i],pla[j])));
d[pla[j].s]=1e9;
}
}
string t;
int w;
vector<pair<string,double> >::iterator it;
cin>>w;
while (w--)
{
cin>>s>>t;
for (it=E[s].begin(); it!=E[s].end(); it++)
{
if(it->first==t)
it->second=0;
}
}
cin>>q;
cout<<"Case "<<ca<<":"<<endl;
while (q--)
{
cin>>s>>t;
for (mit=d.begin(); mit!=d.end(); mit++)
mit->second=1e9;
spfa(s,t);
printf("The distance from %s to %s is %.0lf parsecs.\n",s.c_str(),t.c_str(),d[t]);
}
}
return 0;
}

NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)的更多相关文章

  1. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  2. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  3. poj3013 邻接表+优先队列+Dij

    把我坑到死的题 开始开题以为是全图连通是的最小值 ,以为是最小生成树,然后敲了发现不是,看了下别人的题意,然后懂了: 然后发现数据大,要用邻接表就去学了一下邻接表,然后又去学了下优先队列优化的dij: ...

  4. Head of a Gang (map+邻接表+DFS)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  5. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  6. HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. Prime邻接表+优先队列

    #include <iostream> #include <cmath> #include <cstring> #include <cstdlib> # ...

  8. POJ 3259 Wormholes 邻接表的SPFA判断负权回路

    http://poj.org/problem?id=3259 题目大意: 一个农民有农场,上面有一些虫洞和路,走虫洞可以回到 T秒前,而路就和平常的一样啦,需要花费时间走过.问该农民可不可能从某个点出 ...

  9. 基于STL优先队列和邻接表的dijkstra算法

    首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...

随机推荐

  1. JAVA多线程编程——JAVA内存模型

    一.何为“内存模型” 内存模型描述了程序中各个变量(实例域.静态域和数组元素)之间的关系,以及在实际计算机系统中将变量存储到内存和从内存中取出变量这样的底层细节,对象最终是存储在内存里面的,但是编译器 ...

  2. ASUS主板 Type C 接口无效问题

    修改UEFI设置,把 USB TYPE C POWER SWITCH 改成启用

  3. UVA 12901 Refraction 折射 (物理)

    一道物理题,解个2次方程就行了... 求h最小的情况对应如下图所示 做法不唯一,我想避免精度损失所以在化简的时候尽可能地去避免sqrt和浮点数乘除. 似乎精度要求很低,直接用角度算也可以 #inclu ...

  4. NBear简介与使用图解

    NBear简介与使用图解 框架类型:ORM映射框架 简介:NBear是一个基于.Net 2.0.C#2.0开放全部源代码的的软件开发框架类库.NBear的设计目标是尽最大努力减少开发人员的工作量,最大 ...

  5. python_79_模块定义导入优化

    ''' 1.定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件 (文件名:test.py,对应的模块名:test. import ...

  6. python-下拉框处理

    在自动化中python对下拉框的处理网上相对实例比较少,其它前辈写的教程中对下拉也仅仅是相对与教程来说的,比如下面: m=driver.find_element_by_id("Shippin ...

  7. MVCPager学习小记

    1.PageIndexParameterName怎么关联? 答:其实就是Action里面的pageindex参数 例子: @Html.Pager(Model, new PagerOptions { P ...

  8. 远程连接 mySql数据库

    远程连接 mySql数据库 一.安装并配置MySQL1.安装MySQL:运行mysql-essential-6.0.11-alpha-win32,按“MySQL+6.0+Windows下安装图解”完成 ...

  9. Noip 训练指南

    目录 Noip 训练指南 图论 数据结构 位运算 期望 题解 Noip 训练指南 目前完成 \(4 / 72\) 图论 [ ] 跳楼机 [ ] 墨墨的等式 [ ] 最优贸易 [ ] 泥泞的道路 [ ] ...

  10. 实验二 JSP基本动态元素的使用

    实验二  JSP基本动态元素的使用 实验性质:验证性          实验学时:  2学时      实验地点: 一 .实验目的与要求 1.掌握JSP中声明变量.定义方法.java程序片及表达式的使 ...