【POJ3164】【有向图的最小生成树】【自己的模板】

2024-08-31 16:38

本文主要是介绍【POJ3164】【有向图的最小生成树】【自己的模板】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 14811 Accepted: 4259

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

POJ Monthly--2006.12.31, galaxy



#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_backint n,m;
struct Point
{double x,y;
}P[110];
struct Edge
{int a,b;double c;
}edge[10010];double dis(Point a,Point b)
{return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}double d[110];
int p[110];
int vis[110];
int id[110];double zhuli(int root,int n,int m,Edge edge[])
{double res = 0;while(true){for(int i=0;i<n;i++){d[i] = 0x3f3f3f3f;p[i] = -1;}for(int i=0;i<m;i++){Edge e = edge[i];if(d[e.b] > e.c){d[e.b] = e.c;p[e.b] = e.a;}}for(int i=0;i<n;i++){if(i != root && p[i] == -1) return 0x3f3f3f3f;}int cnt = 0;for(int i=0;i<n;i++){id[i] = -1;vis[i] = -1;}d[root] = 0;for(int i=0;i<n;i++){//if(i != root) res += d[i];int v = i;while(v!= -1 && vis[v] == -1 && v != root) {vis[v] = i;v = p[v];}if(v != root && v != -1 && vis[v] == i){int tt = v;do{id[tt] = cnt;tt = p[tt];}while(tt != v);cnt ++;}}if(cnt == 0) break;for(int i=0;i<n;i++){if(id[i] == -1) id[i] = cnt ++;}for(int i=0;i<m;){int v = edge[i].b;edge[i].a = id[edge[i].a];edge[i].b = id[edge[i].b];if(edge[i].a != edge[i].b){edge[i++].c -= d[v];}else{//edge[i--] = edge[--m];swap(edge[i],edge[--m]);}}n = cnt;root = id[root];}return res;
}
int main()
{freopen("in.txt","r",stdin);while(scanf("%d%d",&n,&m) != EOF){for(int i=0;i<n;i++){scanf("%lf%lf",&P[i].x,&P[i].y);}for(int i=0;i<m;i++){int u,v;scanf("%d%d",&u,&v);u --;v --;//if(u == v) continue;edge[i].a = u;edge[i].b = v;edge[i].c = dis(P[u],P[v]);if(u == v) edge[i].c = 0x3f3f3f3f;}//	cout << "eee" << endl;double ans = zhuli(0,n,m,edge);if(ans == 0x3f3f3f3f){printf("poor snoopy\n");}else{printf("%.2f\n",ans);}}
}


这篇关于【POJ3164】【有向图的最小生成树】【自己的模板】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124431

相关文章

nginx生成自签名SSL证书配置HTTPS的实现

《nginx生成自签名SSL证书配置HTTPS的实现》本文主要介绍在Nginx中生成自签名SSL证书并配置HTTPS,包括安装Nginx、创建证书、配置证书以及测试访问,具有一定的参考价值,感兴趣的可... 目录一、安装nginx二、创建证书三、配置证书并验证四、测试一、安装nginxnginx必须有"-

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java使用POI-TL和JFreeChart动态生成Word报告

《Java使用POI-TL和JFreeChart动态生成Word报告》本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码... 目录前言一、需求背景二、方案分析三、 POI-TL + JFreeChart 实现3.1 Maven

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

Python使用qrcode库实现生成二维码的操作指南

《Python使用qrcode库实现生成二维码的操作指南》二维码是一种广泛使用的二维条码,因其高效的数据存储能力和易于扫描的特点,广泛应用于支付、身份验证、营销推广等领域,Pythonqrcode库是... 目录一、安装 python qrcode 库二、基本使用方法1. 生成简单二维码2. 生成带 Log

基于Java实现模板填充Word

《基于Java实现模板填充Word》这篇文章主要为大家详细介绍了如何用Java实现按产品经理提供的Word模板填充数据,并以word或pdf形式导出,有需要的小伙伴可以参考一下... Java实现按模板填充wor编程d本文讲解的需求是:我们需要把数据库中的某些数据按照 产品经理提供的 word模板,把数据

Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南

《Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南》在日常数据处理工作中,我们经常需要将不同Excel文档中的数据整合到一个新的DataFrame中,以便进行进一步... 目录一、准备工作二、读取Excel文件三、数据叠加四、处理重复数据(可选)五、保存新DataFram

SpringBoot生成和操作PDF的代码详解

《SpringBoot生成和操作PDF的代码详解》本文主要介绍了在SpringBoot项目下,通过代码和操作步骤,详细的介绍了如何操作PDF,希望可以帮助到准备通过JAVA操作PDF的你,项目框架用的... 目录本文简介PDF文件简介代码实现PDF操作基于PDF模板生成,并下载完全基于代码生成,并保存合并P

详解Java中如何使用JFreeChart生成甘特图

《详解Java中如何使用JFreeChart生成甘特图》甘特图是一种流行的项目管理工具,用于显示项目的进度和任务分配,在Java开发中,JFreeChart是一个强大的开源图表库,能够生成各种类型的图... 目录引言一、JFreeChart简介二、准备工作三、创建甘特图1. 定义数据集2. 创建甘特图3.