Command Network
Command Network
Time Limit: 1000MS
Memory Limit: 131072K
Total Submissions: 11970
Accepted: 3482
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
完全是照着别人的代码打出来的,第一道最小树形图
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
const double INF=<<;
const int N=;
const int M=;
int vis[N],id[N];
int n,m,ecnt,pre[N];
double in[N]; struct edge{
int u,v;
double w;
edge( ) {}
edge(int u,int v,double w):u(u),v(v),w(w) {}
}e[M]; struct node
{
double x,y;
}point[N]; double getdis(node a,node b)
{
double x=a.x-b.x;
double y=a.y-b.y;
return sqrt(x*x+y*y);
} double MST(int root,int n,int m)
{
double res=;
while()
{
for(int i = ; i < n; i++)
in[i] = INF;
for(int i = ; i < m; i++)
{
int u = e[i].u;
int v = e[i].v;
if(e[i].w < in[v] && u != v)
{pre[v] = u; in[v] = e[i].w;}
}
for(int i = ; i < n; i++)
{
if(i == root) continue;
if(in[i] == INF) return -;//除了根以外有点没有入边,则根无法到达它
}
//2.找环
int cnt = ;
memset(id, -, sizeof(id));
memset(vis, -, sizeof(vis));
in[root] = ;
for(int i = ; i < n; i++) //标记每个环
{
res += in[i];
int v = i;
while(vis[v] != i && id[v] == - && v != root) //每个点寻找其前序点,要么最终寻找至根部,要么找到一个环
{
vis[v] = i;
v = pre[v];
}
if(v != root && id[v] == -)//缩点
{
for(int u = pre[v]; u != v; u = pre[u])
id[u] = cnt;
id[v] = cnt++;
}
}
if(cnt == ) break; //无环 则break
for(int i = ; i < n; i++)
if(id[i] == -) id[i] = cnt++;
for(int i = ; i < m; i++)
{
int u = e[i].u;
int v = e[i].v;
e[i].u = id[u];
e[i].v = id[v];
if(id[u] != id[v]) e[i].w -= in[v];
}
n = cnt;
root = id[root];
}
return res;
} void solve()
{
for(int i=; i<n; i++) scanf("%lf%lf",&point[i].x,&point[i].y);
ecnt=;
int u,v;
for(int i=; i<m; i++)
{
scanf("%d%d",&u,&v);
if(u==v) continue;
u--; v--;
double dis=getdis(point[u],point[v]);
e[ecnt++]=edge(u,v,dis);
}
double ans=MST(,n,ecnt);
if(ans == -) printf("poor snoopy\n");
else printf("%.2f\n", ans);
} int main()
{
while(scanf("%d%d",&n,&m)>) solve();
return ;
}
Command Network的更多相关文章
- POJ 3164 Command Network 最小树形图
题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
- Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)
链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS Memory Limit: 131072K To ...
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
- POJ3164:Command Network(有向图的最小生成树)
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 20766 Accepted: 5920 ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
- POJ3164 Command Network —— 最小树形图
题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS Memory Limit: 131072K ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
随机推荐
- 实用笔记-EF中直接运行SQL命令
在EF4.1,API的名字 有了些许改变,DbContext.Database就是对应于数据库端信息的封装.执行SQL命令也自然从Database类型开始.对应于ExecuteStoreCommand ...
- Configuring a Windows Azure Project
A Windows Azure project includes two configuration files: ServiceDefinition.csdef and ServiceConfigu ...
- MVC分页之MvcPager使用
最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax异步分页.废话不多说了直接上代码. 一.M ...
- vs2008不能创建C#项目的解决方法
解决方法:1.先关闭 Visual Studio 2008 ;2.在运行中输入命令"devenv.exe /setup"3.运行 Visual Studio 2008 ,一切搞定. ...
- 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性
[源码下载] 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性 作者:webabcd 介绍重新想象 Windows 8. ...
- 与众不同 windows phone (48) - 8.0 其它: C# 调用 C++
[源码下载] 与众不同 windows phone (48) - 8.0 其它: C# 调用 C++ 作者:webabcd 介绍与众不同 windows phone 8.0 之 其它 C# 中调用 W ...
- DoTween小结
using UnityEngine; using System.Collections; using DG.Tweening; public class GetStart : MonoBehaviou ...
- 51Node 1035----最长的循环节
51Node 1035----最长的循环节 正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数. 1/6= 0.1 ...
- git 使用笔记(一)
1. 环境介绍 windows10 2.使用 2.1 安装git for windows 2.2 创建一个文件夹, 开始git管理 2.3 查看该目录,包括隐藏文件 2.4 把testgit.txt添 ...
- python pip 升级每个包
pip本身不自带升级所有包的功能, 但可以通过下面的脚本实现. import pip from subprocess import call for dist in pip.get_installed ...