POJ 3164——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
#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;
}
POJ 3164——Command Network——————【最小树形图、固定根】的更多相关文章
- POJ 3164 Command Network 最小树形图
题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...
- POJ 3164 Command Network 最小树形图模板
最小树形图求的是有向图的最小生成树,跟无向图求最小生成树有很大的区别. 步骤大致如下: 1.求除了根节点以外每个节点的最小入边,记录前驱 2.判断除了根节点,是否每个节点都有入边,如果存在没有入边的点 ...
- POJ 3164 Command Network 最小树形图 朱刘算法
=============== 分割线之下摘自Sasuke_SCUT的blog============= 最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T, ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- poj 3164 Command Network
http://poj.org/problem?id=3164 第一次做最小树形图,看着别人的博客写,还没弄懂具体的什么意思. #include <cstdio> #include < ...
- POJ 3164 Command Network (最小树形图)
[题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...
- POJ 3164 Command Network(最小树形图模板题+详解)
http://poj.org/problem?id=3164 题意: 求最小树形图. 思路: 套模板. 引用一下来自大神博客的讲解:http://www.cnblogs.com/acjiumeng/p ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
随机推荐
- c语言常使用的函数,见到一个记一个
1.strdup() 功能:克隆一个副本,具有独立的内存空间 声明:char *strdup(char *str): 原型: char * __strdup (const char *s) { siz ...
- 添加节点至XML文档中去
不管是<怎样创建XML文档> http://www.cnblogs.com/insus/p/3276944.html还是<泛型List<T>转存为XML文档> ht ...
- Oracle SQL判断字符串是否在目标字符串中的函数
转自:http://dacoolbaby.iteye.com/blog/1772156 根据需求,写了一段方法. 用于识别以下的情况: 判断 字符串A 在用逗号分隔的字符串B中是否存在 如: v_s ...
- 利用Angular2的Observables实现交互控制
在Angular1.x中,我们使用Promise来处理各种异步.但是在angular2中,使用的是Reactive Extensions (Rx)的Observable.对于Promise和Obser ...
- [BZOJ4521][Cqoi2016]手机号码 (数位dp)
题目描述 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号码单独出售.为了便于前 ...
- Vue中添加过渡效果
最近在学习Vue这个框架,发现新的版本中,官网的文档里面说的过渡效果,如果直接粘贴官方的例子中的代码,发现并没有过渡的效果,经过反复测试之后终于知道怎么搞了,把测试的过程总结一下,以便以后回顾. 贴上 ...
- PAT天梯赛 L1-049 天梯赛座位分配
题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...
- Maven入门(二)pom.xml和核心概念
一.pom.xml文件说明 1.pom意思就是project object model. 2.pom.xml包含了项目构建的信息,包括项目的信息.项目的依赖等. 3.pom.xml文件是可以继承的,大 ...
- P5123 [USACO18DEC]Cowpatibility(容斥)
Luogu5123 计算[两组数中有相同的]=\(\sum_{i}\)两组数中\(i\)个数相同的组合方案 map <string,int> 操作\(:\)加上\(,\)防止算重 #inc ...
- 洛谷1541(多维dp)
走格子拿分数,直接弄dp[i]是到了第i格的最大得分可以发现是假的. 于是此题设f[i][j][k][t]代表四种步伐各用了几次可以得到的最大得分,到达的点可以直接算出来,就好转移了. const i ...