zoukankan      html  css  js  c++  java
  • ZOJ 1609 Equivalence(状压+dfs减枝)

    ZOJ Problem Set - 1609
    Equivalence

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    When learning mathamatics, sometimes one may come to an theorem which goes like this:

    The following statements are equivalent:

    a)......
    b)......
    c)......

    For example, let A be an angle between 0 and 360 degrees, the following statements are equivalent:

    a)A = 90 degrees;
    b)A is a right angle;
    c)sin(A) = 1.

    Proving such a theorem is usually a difficult task, because you have to prove that for any two statements Si and Sj, Si concludes Sj and vise versa. Sometimes, proving Si concludes Sj directly is quite difficult so we may find a Sk and prove that Si concludes Sk and Sk concludes Sj. Now given the difficulty of proving every Si => Sj, you are to calculate the minimal total difficulty to prove that the given statements are equivalent.


    Input

    The input contains several cases. Each case begins with an integer n (2 <= n <= 6), the number of statements in this case, followed by n lines, each contains n integers.

    The jth integer of the ith row represents the difficulty of proving Si => Sj. The ith integer of the ith row is always 0 as it's obvious that Si concludes Si. All the n * n integers are between 0 and 100, inclusively. Input is terminated by EOF.


    Output

    For each test case, output a line with the minimal difficulty for that case.


    Sample Input

    4
    0 2 3 4
    5 0 7 8
    9 10 0 12
    13 14 15 0


    Sample Output

    34


    Author: PAN, Minghao
    Source: ZOJ Monthly, May 2003
     

    题解:该题是求强连通,并使权值最小。。。  状压+dfs减枝

    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #include<map>
    #include<set>
    #include<queue>
    #include<cmath>
    #include<string>
    #include<vector>
    using namespace std;
    struct node
    {
        int x,y,w;
    }mp[50];
    int n,cnt,ans;
    int a[1000][10];
    void dfs(int k,int sum)
    {
        if (sum>ans) return; //减枝,如果sum已经大于已保存ans的最优解,说明此方法不是最优,直接返回
        int num=0; //记录到现在为止,能强连通其他点的点的个数
        for(int i=0;i<n;i++)
        {
            num+=a[k][i]==((1<<n)-1); //(1<<n)-1 表达的是所有都连通的状态
            a[k+1][i]=a[k][i];
        }
        if(num==n){ ans=min(ans,sum); return;} //完成了所有的强连通
        if(k>cnt) return; //枚举的路已经没有了
    
        for(int i=0;i<n;i++)
        {
            if(a[k+1][i] & 1<<mp[k].x) //如果i点已经连通mp[k].x,则加上mp[k]这条边,就能连通mp[k].y能连通的点
                a[k+1][i]|=a[k][mp[k].y];
        }
    
        dfs(k+1,sum+mp[k].w);//第k条路取
        for(int i=0;i<n;i++) a[k+1][i]=a[k][i];
        dfs(k+1,sum);//不取
    }
    int main()
    {
    
        while(~scanf("%d",&n))
        {
            cnt=0;
           for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
           {
               int x;
               scanf("%d",&x);
               if (i!=j) mp[cnt].x=i,mp[cnt].y=j,mp[cnt++].w=x;
           }
           cnt--;
          for(int i=0;i<n;i++) a[0][i]=1<<i;
          ans=0x7fffffff;
          dfs(0,0);
          printf("%d
    ",ans);
        }
       return 0;
    }
  • 相关阅读:
    Spring Boot面试杀手锏————自动配置原理
    session在什么时候创建,以及session一致性问题
    IaaS、PaaS、SaaS、DaaS都是什么?现在怎么样了?终于有人讲明白了
    FaaS,未来的后端服务开发之道
    架构师必须了解的30条设计原则
    vuejs2.0使用Sortable.js实现的拖拽功能( 转)
    Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
    Java静态类
    CGLIB(Code Generation Library)详解
    hive--udf函数(开发-4种加载方式)
  • 原文地址:https://www.cnblogs.com/stepping/p/6737975.html
Copyright © 2011-2022 走看看