zoukankan      html  css  js  c++  java
  • POJ 3164 Command Network 最小树形图

    题目链接:

    题目

    Command Network
    Time Limit: 1000MS
    Memory Limit: 131072K

    问题描述

    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.

    输入

    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.

    输出

    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’.

    样例

    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

    output
    31.19
    poor snoopy

    题意

    求固定根的最小树形图

    题解

    朱刘算法

    代码

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    const int maxn = 111;
    const int maxm = 11111;
    const double INF = ~0u >> 1;
    
    struct Edge {
    	int u, v; 
    	double w;
    }egs[maxm];
    
    struct Point {
    	int x, y;
    }pt[maxn];
    
    int n, m;
    
    double dis(const Point& p1, const Point& p2) {
    	return sqrt(1.0*(p1.x - p2.x)*(p1.x - p2.x) + 1.0*(p1.y - p2.y)*(p1.y - p2.y));
    }
    
    double in[maxn];
    int id[maxn], vis[maxn], pre[maxn];
    double Directed_MST(int rt) {
    	double ret = 0;
    	while (1) {
    		//求最小入度边
    		for (int i = 0; i < n; i++) in[i] = INF;
    		for (int i = 0; i < m; i++) {
    			Edge& e = egs[i];
    			if (e.w < in[e.v] && e.u != e.v) {
    				in[e.v] = e.w;
    				pre[e.v] = e.u;
    			}
    		}
    		for (int i = 0; i < n; i++) {
    			if (i!=rt&&in[i] == INF) return -1;
    		}
    		int tot = 0;
    		memset(id, -1, sizeof(id));
    		memset(vis, -1, sizeof(vis));
    		in[rt] = 0;
    		//找环,缩点
    		for (int i = 0; i < n; i++) {
    			ret += in[i];
    			int v = i;
    			while (vis[v] != i&&id[v] == -1 && v != rt) {
    				vis[v] = i;
    				v = pre[v];
    			}
    			if (id[v] == -1 && v != rt) {
    				for (int u = pre[v]; u != v; u = pre[u]) {
    					id[u] = tot;
    				}
    				id[v] = tot++;
    			}
    		}
    		//没有环
    		if (tot == 0) break;
    		for (int i = 0; i < n; i++) {
    			if (id[i] == -1) id[i] = tot++;
    		}
    		//更新到环的距离
    		for (int i = 0; i < m; i++) {
    			Edge& e = egs[i];
    			int v = e.v;//这个v要留下来!
    			e.u = id[e.u],e.v = id[e.v];
    			if (e.u != e.v) {
    				e.w -= in[v];
    			}
    		}
    		n = tot;
    		rt = id[rt];
    	}
    	return ret;
    }
    
    int main() {
    	while (scanf("%d%d", &n, &m) == 2 && n) {
    		for (int i = 0; i < n; i++) {
    			scanf("%d%d", &pt[i].x, &pt[i].y);
    		}
    		for (int i = 0; i < m; i++) {
    			Edge& e = egs[i];
    			scanf("%d%d", &e.u, &e.v),e.u--,e.v--;
    			if (e.u != e.v) e.w = dis(pt[e.u], pt[e.v]);
    			else e.w = INF;
    		}
    		double ans = Directed_MST(0);
    		if (ans ==-1) {
    			puts("poor snoopy");
    		}
    		else {
    			printf("%.2f
    ", ans);
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    0x80070522:客户端没有所需的特权的解决方法(win7,win10通过)
    asp类型转换函数汇总 转贴
    web开发中 代码解决部分IE兼容问题
    关于系统弹出错误:429 , ActiveX 部件不能创建对象 的解决方法
    关于奥威亚自动录播系统的设置使用小笔记
    网络克隆--机房利器(acer自带还原)
    raid的一些简单知识
    sharepoint 2013 sp1 patch安装后的手工运行
    Sharepoint 2013 多服务器域的目录服务器和搜索服务的配置
    加速安装 Sharepoint 2013 SP1
  • 原文地址:https://www.cnblogs.com/fenice/p/5641845.html
Copyright © 2011-2022 走看看