Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)
链接:
http://poj.org/problem?id=3164
题目:
Command Network
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 ‘ Sample Input 4 6 Sample Output 31.19 Source POJ Monthly--2006.12.31, galaxy
|
#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 ;
}
Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)的更多相关文章
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- POJ2135 最小费用最大流模板题
练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题
Have you ever read any book about treasure exploration? Have you ever see any film about treasure ex ...
- POJ 1459 Power Network(网络最大流,dinic算法模板题)
题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数. 接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
- 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 ...
随机推荐
- OpenCV参考手册之Mat类详解
OpenCV参考手册之Mat类详解(一) OpenCV参考手册之Mat类详解(二) OpenCV参考手册之Mat类详解(三)
- 【SCOI2009】迷路
题面 题解 如果给我们的是一个邻接矩阵,那么直接给邻接矩阵\(T\)次幂即可. 这里的图有边权,那么我们就将它拆成\(9\)个点即可. 代码 #include<cstdio> #inclu ...
- HTML基本代码教学,第三天
HTML 今天由于个人情况,身体不适,但是为了大家的学习进度,咱们以纯文字得形式来简单了解下今天的学习内容 今儿咱们来了解下表单 <form id=" " name=&qu ...
- Spring Cloud Learning(一): 服务注册
官网https://projects.spring.io/spring-cloud/,spring cloud官网各组件版本为: Component Edgware.SR4 Finchley.SR1 ...
- requests,unittest——多接口用例,以及需要先登录再发报的用例
之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...
- 十几行代码带你用Python批量实现txt转xls,方便快捷
前天看到后台有一兄弟发消息说目前自己有很多txt 文件,领导要转成xls文件,问用python怎么实现,我在后台简单回复了下,其实完成这个需求方法有很多,因为具体的txt格式不清楚,当然如果是有明确分 ...
- 【python 3.6】如何将list存入txt后,再读出list
今天遇到一个需求,就是将一个list文件读取后,存入一个txt配置文件.存入时,发现list文件无法直接存入,必须转为str模式. 但在读取txt时,就无法恢复成list类型来读取了(准确地说,即使强 ...
- cinder创建volume的流程-简单梳理
1. cinder-api接收到创建的请求,入口:cinder.api.v2.volumes.VolumeController#create,该方法主要负责一些参数的重新封装和校验,然后调用cinde ...
- IBM基于Kubernetes的容器云全解析
基于Kubernetes的容器云 容器云最主要的功能是以应用为中心,帮助用户把所有的应用以容器的形式在分布式里面跑起来,最后把应用以服务的形式呈现给用户.容器云里有两个关键点,一是容器编排,二是资源调 ...
- spark的运行方式——转载
本文转载自: spark的运行方式 本文主要讲述运行spark程序的几种方式,包括:本地测试.提交到集群运行.交互式运行 等. 在以下几种执行spark程序的方式中,都请注意master的设 ...