zoukankan      html  css  js  c++  java
  • POJ 3164 Command Network

    Command Network

    Time Limit: 1000ms
    Memory Limit: 131072KB
    This problem will be judged on PKU. Original ID: 3164
    64-bit integer IO format: %lld      Java class name: Main
     

    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

     
    解题:最小树形图
     
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 const int INF = 0x3f3f3f3f;
     8 const int maxn = 200;
     9 struct arc {
    10     int u,v;
    11     double w;
    12 } e[maxn*maxn];
    13 struct Point {
    14     int x,y;
    15 } p[maxn];
    16 double calc(int a,int b) {
    17     double tmp = (p[a].x - p[b].x)*(p[a].x - p[b].x);
    18     tmp += (p[a].y - p[b].y)*(p[a].y - p[b].y);
    19     return sqrt(tmp);
    20 }
    21 double in[maxn];
    22 int pre[maxn],hs[maxn],vis[maxn];
    23 double DMST(int root,int n,int m) {
    24     double ret = 0;
    25     while(true) {
    26         for(int i = 0; i < n; ++i) {
    27             in[i] = INF;
    28             hs[i] = vis[i] = -1;
    29         }
    30         for(int i = 0; i < m; ++i) {
    31             if(e[i].u != e[i].v && e[i].w < in[e[i].v]) {
    32                 in[e[i].v] = e[i].w;
    33                 pre[e[i].v] = e[i].u;
    34             }
    35         }
    36         for(int i = 0; i < n; ++i)
    37             if(i != root && in[i] == INF) return -1;
    38         in[root] = 0;
    39         int cnt = 0;
    40         for(int i = 0; i < n; ++i) {
    41             ret += in[i];
    42             int v = i;
    43             while(vis[v] != i && hs[v] == -1 && v != root) {
    44                 vis[v] = i;
    45                 v = pre[v];
    46             }
    47             if(v != root && hs[v] == -1) {
    48                 for(int u = pre[v]; u != v; u = pre[u]) hs[u] = cnt;
    49                 hs[v] = cnt++;
    50             }
    51         }
    52         if(!cnt) break;
    53         for(int i = 0; i < n; ++i)
    54             if(hs[i] == -1) hs[i] = cnt++;
    55         for(int i = 0; i < m; ++i) {
    56             int u = e[i].u,v = e[i].v;
    57             e[i].u = hs[u];
    58             e[i].v = hs[v];
    59             if(u == v) continue;
    60             e[i].w -= in[v];
    61         }
    62         n = cnt;
    63         root = hs[root];
    64     }
    65     return ret;
    66 }
    67 int main() {
    68     int n,m;
    69     while(~scanf("%d%d",&n,&m)) {
    70         for(int i = 0; i < n; ++i)
    71             scanf("%d%d",&p[i].x,&p[i].y);
    72         for(int i = 0; i < m; ++i) {
    73             scanf("%d%d",&e[i].u,&e[i].v);
    74             if(--e[i].u == --e[i].v) e[i].w = INF;
    75             else e[i].w = calc(e[i].u,e[i].v);
    76         }
    77         double ret = DMST(0,n,m);
    78         if(ret == -1) puts("poor snoopy");
    79         else printf("%.2f
    ",ret);
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    HO引擎近况20210912
    查询超时问题的处理
    ubuntu根据关键词批量杀进程
    创建notebook适用的虚拟环境
    信赖域策略优化(Trust Region Policy Optimization, TRPO)
    强化学习(Reinforcement Learning)
    生成对抗网络(GAN与W-GAN)
    卷积神经网络CNN
    循环神经网络RNN
    PyTorch自动求导
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4761567.html
Copyright © 2011-2022 走看看