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 ...
随机推荐
- SaltStack入门篇(一)之SaltStack部署
一.SaltStack概述 Salt,,一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯. salt底层采用动态的连接总线, ...
- Entity Framework中执行Sql语句
如果想在EF框架中执行Sql语句,其实很简单,EF里面已经提供了相关的方法(此处使用的EF为EF4.1版本). EF中提供了两个方法,一个是执行查询的Sql语句SqlQue ...
- 【JUC源码解析】ReentrantReadWriteLock
简介 ReentrantReadWriteLock, 可重入读写锁,包括公平锁和非公平锁,相比较公平锁而言,非公平锁有更好的吞吐量,但可能会出现队列里的线程无限期地推迟一个或多个读线程或写线程的情况, ...
- 180730-Spring之RequestBody的使用姿势小结
Spring之RequestBody的使用姿势小结 SpringMVC中处理请求参数有好几种不同的方式,如我们常见的下面几种 根据 HttpServletRequest 对象获取 根据 @PathVa ...
- Appium+python 自动发送邮件(1)(转)
(原文:https://www.cnblogs.com/fancy0158/p/10056091.html) SMTP:简单传输协议,实在Internet上传输Email的事实标准. Python的s ...
- idea项目 run、debug变灰色的问题
点击如图所示位置的下来三角按钮,然后选择Edit Configurations,或者点击菜单栏Run>Edit Configurations 2 在运行配置窗口,选择一条springboot的运 ...
- PHP版本的讲解
原文地址:http://dev.meettea.com/show-90-1.html 最近发现很多PHP程序员对PHP版本知识了解不是很清楚,其中不乏PHP产品主力开发人员. PHP版本主要分三支:P ...
- 建立 Python 虚拟环境
1.1 安装依赖包 $ yum -y install wget gcc epel-release git 1.2 安装 Python3.6和pip $ yum -y install python36 ...
- [kuangbin带你飞]专题一 简单搜索 回顾总结
第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队
- leetcode27_C++Remove Element
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...