4525: [Cerc2012]Kingdoms

4525: [Cerc2012]Kingdoms

 

meaning of the title

  N countries, there may be debt or debt relationship between two, one country goes bankrupt: its expenditure is greater than its income. Ask if a country can hold out to the end.

ideas

  Very interesting question.

  dp[s] indicates that there is a situation in the current country as s, 1-survival, 0-bankrupt. Then the start is 11111.... Then enumerate a country and judge whether it can go bankrupt and transfer.

  Finally, scan through all cases with only 1.

code

 

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5  
 6 using namespace std;
 7 
 8 const int N = 30;
 9 int d[N][N],f[(1<<21)+10];
10 
11 inline int read() {
12     int x = 0,f = 1;char ch = getchar();
13     for (; !isdigit(ch); ch=getchar()) if(ch=='-') f=-1;
14     for (; isdigit(ch); ch=getchar()) x = x*10+ch-'0';
15     return x*f; 
16 }
17 
18 int main() {
19     int T = read();
20     while (T--) {
21         int n = read();
22         for (int i=1; i<=n; ++i) 
23             for (int j=1; j<=n; ++j) 
24                 d[i][j] = read();
25         int MaxS = (1<<n)-1; 
26         for (int i=0; i<=MaxS; ++i) f[i] = false;
27         f[MaxS] = true;
28         for (int s=MaxS; s>=1; --s) {
29             if (!f[s]) continue;
30             int ans = 0;
31             for (int i=1; i<=n; ++i) {
32                 if (!(s&(1<<(i-1)))) continue;
33                 int sum = 0 ;
34                 for (int j=1; j<=n; ++j) 
35                     if (s&(1<<(j-1))) sum += d[j][i]; //j欠i=>i可以获得的 
36                 if (sum < 0) f[s^(1<<(i-1))] = true; 
37             }
38         }
39         bool flag = true;
40         for (int i=1; i<=n; ++i) {
41             if (f[(1<<(i-1))]) {
42                 if (flag) printf("%d",i),flag = false;
43                 else printf(" %d",i);
44             }
45         }
46         if (flag) printf("0");
47         puts(""); 
48     }
49     return 0;
50 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214918&siteId=291194637