zoukankan      html  css  js  c++  java
  • POJ3164 Command Network

    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 17235   Accepted: 4959

    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

    图论 最小树形图

    朱刘算法模板题

    先统计出每个点的最小入边,如果这些边之间没有形成环,就可以把它们都当做生成树的边,直接累加权值出解。

    否则,先把当前所有最小入边的权值累加起来,作为答案的一部分,

    如果形成了环,可能有更优解,那么就需要缩点。

    缩点后,改变边的指向,边<u,v>的边权减去原先v的最小入边权值。(之后如果选这条边,相当于自动减去环上连到v的边的权值,类似于“反悔”操作)

    重新统计每个点的最小入边,重新判环……直到图中没有环。

    http://blog.csdn.net/sdj222555/article/details/7459738

    ↑这里讲得挺好。

    发现读入优化会被EOF卡掉……还是得用scanf

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 using namespace std;
     9 const double INF=1e9;
    10 const int mxn=110;
    11 int read(){
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 struct point{
    18     double x,y;
    19 }p[mxn];
    20 double dist(point a,point b){
    21     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    22 }
    23 struct edge{
    24     int u,v;
    25     double w;
    26 }e[mxn*mxn];
    27 int id[mxn],pre[mxn],vis[mxn];
    28 double in[mxn];
    29 int n,m;
    30 double DMST(int root,int V,int E){
    31     double res=0;
    32     int i,j;
    33     while(1){
    34         //统计最小入边 
    35         for(i=1;i<=V;i++)in[i]=INF;
    36         for(i=1;i<=E;i++){
    37             int u=e[i].u,v=e[i].v;
    38             if(in[v]>e[i].w && u!=v){//找最小入边,记录前驱 
    39                 in[v]=e[i].w;
    40                 pre[v]=u;
    41             }
    42         }
    43         for(i=1;i<=V;i++){
    44             if(i==root)continue;
    45             if(in[i]==INF)return -1;//除根节点外若有某点没有入边,无解 
    46         }
    47         //找环
    48         int cnt=0;
    49         memset(id,0,sizeof id);
    50         memset(vis,0,sizeof vis);
    51         in[root]=0;
    52         for(i=1;i<=V;i++){
    53             res+=in[i];
    54             int v=i;
    55             while(vis[v]!=i && !id[v] && v!=root){
    56                 vis[v]=i;
    57                 v=pre[v];
    58             }
    59             if(v!=root && !id[v]){//有环则缩点 
    60                 ++cnt;
    61                 for(int u=pre[v];u!=v;u=pre[u])id[u]=cnt;
    62                 id[v]=cnt;
    63             }
    64         }
    65         if(cnt==0)break;//无环时退出
    66         //建立新图 
    67         for(i=1;i<=V;i++)if(!id[i])id[i]=++cnt;
    68         for(i=1;i<=E;i++){
    69             int u=e[i].u,v=e[i].v;
    70             e[i].u=id[u];
    71             e[i].v=id[v];
    72             if(id[u]!=id[v])e[i].w-=in[v];
    73         }
    74         V=cnt;//更新结点数
    75         root=id[root]; 
    76     }
    77     return res;
    78 }
    79 int main(){
    80     int i,j;
    81     while(scanf("%d%d",&n,&m)!=EOF){
    82         for(i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
    83         for(i=1;i<=m;i++){
    84             scanf("%d%d",&e[i].u,&e[i].v);
    85 //            e[i].u=read();
    86 //            e[i].v=read();
    87             if(e[i].u!=e[i].v)e[i].w=dist(p[e[i].u],p[e[i].v]);
    88                 else e[i].w=INF;
    89         }
    90         double ans=DMST(1,n,m);
    91         if(ans==-1)printf("poor snoopy
    ");
    92         else printf("%.2f
    ",ans);
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    2015年5月1日 转载--各种变量在内存中的分布
    2015年4月30日 计算两个日期天数,写的代码
    2015年4月29日 dayofweek
    2015年4月28日
    2015年4月28日----高大上的数组,进制准换,最多是35进制
    2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
    2015年4月27日
    《C++ Primer》学习笔记:迭代器介绍
    《C++ Primer》学习笔记:3.3.3其他vector操作
    《C++ Primer》学习笔记:向vector对象添加元素蕴含的编程假定
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6275785.html
Copyright © 2011-2022 走看看