zoukankan      html  css  js  c++  java
  • 2014浙江省赛 ZOJ

    ---恢复内容开始---

    2014浙江省赛  ZOJ - 3777 B-Problem Arrangement

    https://cn.vjudge.net/problem/ZOJ-3777

    Time limit2000 msM

    emory limit65536 kB

    OSLinux

    SourceThe 11th Zhejiang Provincial Collegiate Programming ContestAuthorDAI, Longao

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward is going to arrange the order of the problems. As we know, the arrangement will have a great effect on the result of the contest. For example, it will take more time to finish the first problem if the easiest problem hides in the middle of the problem list.

    There are N problems in the contest. Certainly, it's not interesting if the problems are sorted in the order of increasing difficulty. Edward decides to arrange the problems in a different way. After a careful study, he found out that the i-th problem placed in the j-th position will add Pij points of "interesting value" to the contest.

    Edward wrote a program which can generate a random permutation of the problems. If the total interesting value of a permutation is larger than or equal to Mpoints, the permutation is acceptable. Edward wants to know the expected times of generation needed to obtain the first acceptable permutation.

    Input

    There are multiple test cases. The first line of input contains an integer Tindicating the number of test cases. For each test case:

    The first line contains two integers N (1 <= N <= 12) and M (1 <= M <= 500).

    The next N lines, each line contains N integers. The j-th integer in the i-th line is Pij (0 <= Pij <= 100).

    <h4< dd="">Output

    For each test case, output the expected times in the form of irreducible fraction. An irreducible fraction is a fraction in which the numerator and denominator are positive integers and have no other common divisors than 1. If it is impossible to get an acceptable permutation, output "No solution" instead.

    <h4< dd="">Sample Input

    2
    3 10
    2 4 1
    3 2 2
    4 5 3
    2 6
    1 3
    2 4
    

    <h4< dd="">Sample Output

    3/1
    No solution

    分析

      算是八皇后问题的拓展吧,在n*n的矩阵中选择n个点,每行每列只选择一个,把每个点的权值相加要求大于m,问多少种方案,这题是队友写的,我没有什么思路,在赛后队友讲题后,看队友的代码的时候理解的st 的意思,也就是状态压缩,用二进制和位运算来表示搜素的状态,是一个很好的思路,配合搜索理解起来更容易了,每次从一个二进制n位的数字中选择一位变成1,最终的结果就是n个1,在检查是否大于,中间再配合剪枝等操作就可以了,感觉状态压缩和并查集这种东西都是我对这些东西都太害怕了,一直裹足不前不敢尝试,理解了之后才明白原来只是这个样子的,一点一点的学习,加油吧。

    AC代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 int n, m;
     6 int arr[15][15];
     7 int tmp[20] = {1,1,2,6,};
     8 int gcd(int a,int b){
     9     int t = b;
    10     while(b){
    11         b = a % b;
    12         a = t;
    13         t = b;
    14     }
    15     return a;
    16 }
    17 int dp[1<<12][1205];
    18 
    19 int dfs(int d,int st,int sum){
    20     if(d>=n)
    21         return sum>=m;
    22     if(sum >= m)
    23         return tmp[n-d];
    24     if(dp[st][sum] != -1)
    25         return dp[st][sum];
    26     int cnt = 0;
    27     for(int i=0;i<n;i++)if( (st>>i&1) == 0){
    28         //vis[i] = 1;
    29         cnt += dfs(d+1,(st | (1<<i)),sum+arr[d][i]);
    30         //vis[i] = 0;
    31     }
    32     return dp[st][sum] = cnt;
    33 }
    34 int main(){
    35     int t;
    36     scanf("%d",&t);
    37     for(int i=2;i<13;i++)
    38         tmp[i] = tmp[i-1]*i;
    39     while(t--){
    40         memset(dp,-1,sizeof(dp));
    41         scanf("%d%d",&n,&m);
    42         for(int i=0;i<n;i++)
    43         for(int j=0;j<n;j++){
    44             scanf("%d",&arr[i][j]);
    45         }
    46         int cnt = dfs(0,0,0);
    47         int g = __gcd(tmp[n],cnt);
    48         if(cnt)
    49             printf("%d/%d
    ",tmp[n]/g,cnt/g);
    50         else
    51             puts("No solution");
    52     }
    53     return 0;
    54 }
    55 
    56 /*
    57 
    58 2
    59 3 10
    60 2 4 1
    61 3 2 2
    62 5 4 3
    63 2 6
    64 1 3
    65 2 4
    66 
    67 */
  • 相关阅读:
    安装nginx后启动提示缺少libjemalloc.so.2
    页面刷新后保持滚动条的位置
    mysql的tinyint字段返回布true / false的问题
    MySql处理死锁的解决方案
    apidoc使用记录
    微信公众号开发图片上传案例
    [ Error 分析] Comparison method violates its general contract!
    [intellij]create gradle project
    [重构]读书笔记
    [设计模式]迭代子模式 Iterator
  • 原文地址:https://www.cnblogs.com/Kidgzz/p/10825463.html
Copyright © 2011-2022 走看看