zoukankan      html  css  js  c++  java
  • HDOJ 4085 Peach Blossom Spring

    discription
    Tao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous works is "Peach Blossom Spring", which is a fable about a chance 
    discovery of an ethereal village where the people lead an ideal existence in harmony with nature, unaware of the outside world for centuries. So in Chinese, "Peach Blossom Spring" means "utopia". 
    In the story of "Peach Blossom Spring", there was a mysterious place. In Qin dynasty, some people escaped to that place during the civil unrest and built a village. They and their descendants never left and never had any contact with the outside world since then, until centuries latter a fisherman of Jin dynasty found them. 
    Recently, some Chinese ACMers happened to find the relics of the village mentioned in"Peach Blossom Spring". 
    They also found a document about building hiding places to escape from Qin army. The document said: 
    There were n houses and m roads in the village. Each road connected two houses. These houses were numbered from 1 to n. There were k families, each living in a different house. 
    The houses they lived were house 1, house 2, … , house k. There were also k broken houses: house n-k+1, house n-k+2, ... , house n, with secret basements so that those houses could be used as hiding places. 
    The problem was that all roads were broken. People wanted to repair some roads so that every family could reach a hiding place through the repaired roads. Every hiding place could only hold one family. Each road cost some labor to be repaired. The head of the village wanted to find out the minimum cost way of repairing the roads, but he didn't know how to do. 
    Would you solve the problem which the ancient village head never solved?

    Input

    The input begins with a line containing an integer T(T<=50), the number of test cases. For each case, the first line begins with three integers ---- the above mentioned n (4<=n<=50), m (0<=m<=1000) and k (1<=k<=5, 2k<=n). Then m lines follow, each containing three integers u,v and w, indicating that there is a broken road connecting house u an d v, and the cost to repair that road is w(1<=w<=1000).

    Output

    For each test case, if you cannot find a proper way to repair the roads, output a string "No solution" in a line. Otherwise, output the minimum cost to repair the roads in a line.

    Sample Input

    2
    4 3 1
    4 2 10
    3 1 9
    2 3 10
    6 7 2
    1 5 1000
    2 6 1000
    1 3 1
    2 3 1
    3 4 1
    4 5 1
    4 6 1

    Sample Output

    29
    5

    和上一道斯坦纳树的很像啊,就是关键点一一对应变成了数量对应,其实总体上写法都差不多。
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cstring>
    #define ll long long
    using namespace std;
    int f[1100][105],n,m,T,ci[30];
    int ans[1100],dis[105],k;
    int q[100005],l,r,num,all;
    int to[2005],ne[2005];
    int val[2005],hd[105];
    bool iq[105];
    
    inline void init(){
        num=0;
        memset(hd,0,sizeof(hd));
        memset(f,0x3f,sizeof(f));
        memset(ans,0x3f,sizeof(ans));
        memset(f[0],0,sizeof(f[0]));
    }
    
    int main(){
        ci[0]=1;
        for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
        
        scanf("%d",&T);
        while(T--){
            init();
            scanf("%d%d%d",&n,&m,&k);
            for(int i=1;i<=k;i++) f[ci[i-1]][i]=0;
            for(int i=n-k+1;i<=n;i++) f[ci[n+k-i]][i]=0;
            
            int uu,vv,ww;
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&uu,&vv,&ww);
                to[++num]=vv,ne[num]=hd[uu],hd[uu]=num,val[num]=ww;
                to[++num]=uu,ne[num]=hd[vv],hd[vv]=num,val[num]=ww;        
            }
            
            all=ci[k<<1]-1;
            for(int S=1;S<=all;S++){
                for(int i=1;i<=n;i++)
                    for(int s=S&(S-1);s;s=(s-1)&S) f[S][i]=min(f[S][i],f[s][i]+f[s^S][i]);
                
                for(int i=1;i<=n;i++) dis[i]=f[S][i],iq[i]=1,q[i]=i;
                l=1,r=n;
                while(l<=r){
                    int x=q[l++];
                    for(int i=hd[x];i;i=ne[i]) if(dis[x]+val[i]<dis[to[i]]){
                        dis[to[i]]=dis[x]+val[i];
                        if(!iq[to[i]]) iq[to[i]]=1,q[++r]=to[i];
                    }
                    iq[x]=0;
                }
                
                for(int i=1;i<=n;i++) f[S][i]=dis[i];
            }
            
            for(int s=0;s<=all;s++){
                int num1=0,num2=0;
                for(int i=1;i<=k;i++) if(s&ci[i-1]) num1++;
                for(int i=k+1;i<=(k<<1);i++) if(s&ci[i-1]) num2++;
                if(num1!=num2) continue;
                
                for(int i=1;i<=n;i++) ans[s]=min(ans[s],f[s][i]);
            }
            
            for(int S=1;S<=all;S++)
                for(int s=S&(S-1);s;s=(s-1)&S) ans[S]=min(ans[S],ans[s]+ans[s^S]);
            
            if(ans[all]!=ans[all+1]) printf("%d
    ",ans[all]);
            else puts("No solution");
        }
        
        return 0;
    }
  • 相关阅读:
    Java中的几种常用循环 for switch while dowhile
    HTML的各种基本标签
    2017年终总结
    HTML C# ajax结合ashx处理程序实现文件上传
    HTML div鼠标悬停控制子控件显示与隐藏
    HTML 使用CSS 如何去掉文本聚焦框
    HTML input 文本框如何添加提示信息
    CSS 如何通过top left 定位控制div在另一个div的位置
    CSS background 属性
    php支付接口开发-支付宝-开发前期准备
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8303146.html
Copyright © 2011-2022 走看看