Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 15080   Accepted: 4331

Description

After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow Mlines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.

Sample Input

4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3

Sample Output

31.19
poor snoopy

Source

 
 
题目大意:问你起点在1,存不存在最小有向生成树,如果存在输出结果,如果不存在,输出poor Snoopy。
 
解题思路:固定根的最小树形图。直接用朱刘算法套上就能过了。    注意点:1.如果是不定根,可以自己加一个起点,然后让起点跟其他点连边。2.不能记录路径。
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Coor{
double x,y;
}coors[maxn];
struct Edge{
int from,to;
double dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
double In[maxn];
double distan(Coor a,Coor b){
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt( dx * dx + dy * dy);
}
double Zhuliu(int root,int n,int m){
double ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = 1.0*INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
ret += In[i];
v = i;
while(vis[v]!= i && ID[v] ==-1 &&v != root){
vis[v] = i;
v = pre[v];
}
if(v != root && ID[v] == -1){
for(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 0; i < n; i++){
scanf("%lf%lf",&coors[i].x,&coors[i].y);
}
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
a--,b--;
edges[i].from = a;
edges[i].to = b;
if(a == b){
edges[i].dist = 1.0*INF;
continue;
}
edges[i].dist = distan(coors[a],coors[b]);
}
double res = Zhuliu(0,n,m);
if(res == -1){
puts("poor snoopy");
}else{
printf("%.2f\n",res);
}
}
return 0;
}

  

最新文章

  1. hdu 3632 A Captivating Match(区间dp)
  2. YHLMR007 服装PDA验货差异报表
  3. CentOS下判断自己的VPS是OpenVZ的还是Xen的
  4. Cadence ORCAD CAPTURE元件库介绍
  5. chrpath工具使用
  6. CCNA第三讲笔记
  7. linux下avr单片机开发:中断服务程序
  8. Java语言基础(五)
  9. codeforces 100548F (西安现场赛F题):容斥原理
  10. html加载js那些事
  11. C#日志工具汇总
  12. [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
  13. ActionBar更改背景颜色(主题)
  14. 利用Dockerfile构建mysql镜像并实现数据的初始化及权限设置
  15. CentOS 7 安装.NET Core 2.0
  16. Python线程模块threading
  17. server 打开失败
  18. WinDbg分析Dump常用方法和命令
  19. Robot Framework浏览器驱动下载
  20. Jquery 中 ajaxSubmit使用讲解

热门文章

  1. 添加节点至XML文档中去
  2. Python3中集合的混合使用
  3. 洛谷P3648 [APIO2014]序列分割(斜率优化)
  4. nginx的worker_processes优化
  5. 老男孩Day6作业:计算器
  6. P3749 [六省联考2017]寿司餐厅 最小割
  7. opencv第二课,使用cmake编译OpenCV,并添加opencv_contrib模块
  8. kuangbin专题七 HDU1754 I Hate It (单点修改维护最大值)
  9. xlrd和xlwd模块
  10. 懒汉式单例要加volatile吗