链接:

http://poj.org/problem?id=3164

题目:

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8922   Accepted: 2609

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 M lines 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

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m;
int vis[maxn], inc[maxn], pre[maxn];
double w[][]; struct edge
{
int x, y;
}Edge[maxn]; void dfs(int u)
{
vis[u] = ;
for(int i=; i<=n; i++)
if(!vis[i] && w[u][i] < INF)
dfs(i);
} double dirmst(int u)
{
double ans = ;
//== 步骤1: 判断能否形成最小树形图,直接dfs遍历 (就是检验一下图是否能够联通)
dfs(u);
for(int i=; i<=n; i++)
if(!vis[i])
return -;
//== 如果可以形成最小树形图,继续
mem(vis, );
while(true)
{
//== 1. 找最小前驱边
for(int i=; i<=n; i++) if(i != u && !inc[i]){
w[i][i] = INF; pre[i] = i;
for(int j=; j<=n; j++) if(!inc[j] && w[j][i] < w[pre[i]][i])
pre[i] = j;
}
//== 2.判断是否有环
int i;
for(i=; i<=n; i++) if(i != u && !inc[i]){
int j = i, cnt = ;
while(j != u && pre[j] != i && cnt <= n) j = pre[j], ++cnt;
if(j == u || cnt > n) continue;
break;
}
//== 没有找到环,得到答案
if(i > n)
{
for(int i=; i<=n; i++) if(i != u && !inc[i]) ans += w[pre[i]][i];
return ans;
}
//== 有环,则对这个环进行收缩
int j = i;
mem(vis, );
do{
ans += w[pre[j]][j], j = pre[j], vis[j] = inc[j] = true;
}while(j != i);
inc[i] = false; // 环缩成了点i,点i仍然存在 for(int k=; k<=n; k++) if(vis[k]){ //在环中的点
for(int j=; j<=n; j++) if(!vis[j]){ //不在环中的点
if(w[i][j] > w[k][j]) w[i][j] = w[k][j]; //更新环的出边
if(w[j][k] < INF && w[j][k] - w[pre[k]][k] < w[j][i]) //更新换的入边
w[j][i] = w[j][k] - w[pre[k]][k];
}
}
}
return ans;
} void init()
{
mem(vis, );
mem(inc, );
rap(i, , n)
rap(j, i, n)
w[i][j] = w[j][i] = INF;
} int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
rap(i, , n)
{
scanf("%d%d", &Edge[i].x, &Edge[i].y);
}
rap(i, , m)
{
int a, b;
scanf("%d%d", &a, &b);
double c = sqrt((double)(Edge[a].x - Edge[b].x)*(Edge[a].x - Edge[b].x) + (double)(Edge[a].y - Edge[b].y)*(Edge[a].y - Edge[b].y));
if(w[a][b] > c)
w[a][b] = c;
}
double ans = dirmst();
if(ans < ) puts("poor snoopy");
else printf("%.2f\n", ans); } return ;
}

最新文章

  1. yii2使用相关记录
  2. U盘格式化后的恢复
  3. cocos2d中的可见性检测
  4. 20145227 《Java程序设计》第9周学习总结
  5. [经典算法] 排列组合-N元素集合的M元素子集
  6. idea sass scss配置
  7. Python你必须知道的十个库
  8. 初试weka数据挖掘
  9. MVC,MVP,MVVM
  10. 删除bin后,Eclipse重新编译项目
  11. ASP.NET MVC Filter的思考
  12. HI258摄像头旋转配置问题
  13. QEMU KVM Libvirt手册(6) &ndash; Network Block Device
  14. 『宝藏 状态压缩DP NOIP2017』
  15. python在使用MySQLdb模块时报Can&#39;t extract file(s) to egg cacheThe following error occurred while trying to extract file(s) to the Python eggcache的错误。
  16. java实现 HTTP/HTTPS请求绕过证书检测代码实现
  17. python记录_day01 初始
  18. qurtz.net(转载)
  19. Oracle EBS OPM convert dtl reservation
  20. Docker之My sql 之旅

热门文章

  1. Linux日志切割logrotate服务配置
  2. [flex 布局]——flex教程
  3. Ubuntu设置DNS服务
  4. 3星|麦肯锡合伙人《从1到N》:PPT讲稿,图表不错,讲解不够深入
  5. 简析Monte Carlo与TD算法的相关问题
  6. 华为笔试——C++特定位数比较
  7. 使用 Mesos 管理虚拟机
  8. hbase Problem binding to node1/192.168.1.13:16020 : 地址已在使用
  9. JavaScript DOM编程艺术学习笔记-第一章JavaScript简史
  10. 使用RStudio学习一个简单神经网络