A - Bus Planning Kattis - busplanning (状压DP)

Topic links:

https://cn.vjudge.net/problem/Kattis-busplanning

Subject to the effect:

Own translation

Specific ideas:

Asked the other, is not my idea ,,

Binary enumeration of this subset of n people, is able to determine which of several states in a car, and then determine which of the two states for all his sub-set of complementary who can make this state a minimum.

Each status record which is composed of a subset, to facilitate output.

What VIS [i], i represents a subset of states is

AC Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define inf 0x3f3f3f3f
 5 #define LL_inf (1ll << 60)
 6 const int maxn = 1e6 + 100;
 7 const int mod = 1e9 + 7;
 8 vector<int>Edge[maxn];
 9 map<string,int>sto;
10 map<int,string>un_sto;
11 int dp[maxn];
12 int n,k,c;
13 int vis[maxn];
14 map<int,int>q;
15 int cal(int sta)
16 {
17     q.clear();
18     vector<int>tmp;
19     for(int i=0; i<n; i++)
20     {
21         if((1ll<<i)&sta)
22             tmp.push_back(i),q[i]=1;
23     }
24     if(tmp.size()>c)return inf ;
25     int len=tmp.size();
26     for(int i=0; i<len; i++)
27     {
28         for(int j=0; j<Edge[tmp[i]].size(); j++)
29         {
30             if(q[Edge[tmp[i]][j]])
31             {
32                 return inf ;
33             }
34         }
35     }
36     return . 1 ;
 37 [  }
 38 is  void DFS ( int U)
 39  {
 40      IF (DP [U] == . 1 ) // If this subset may be placed on a car
 41 is      {
 42 is          for ( int I = 0 ; I <n-; ++ I )
 43 is          {
 44 is              IF (( . 1 << I) & U)
 45                  COUT << un_sto [I] << "  " ;
 46 is          }
 47          COUT << endl;
 48          return ;
 49      }
50      DFS (U ^ VIS [U]); // search in accordance with his subset down
 51 is      DFS (VIS [U]);
 52 is  }
 53 is  int main ()
 54 is  {
 55      String STR;
 56 is      CIN n->> >> >> K C;
 57 is      for ( int I = 0 ; I <n-; I ++ )
 58      {
 59          CIN >> STR;
 60          STO [STR] = I;
 61 is          un_sto [I] = STR;
 62 is      }
 63 is      String str1, str2 ;
 64      for ( int i=1; i<=k; i++)
65     {
66         cin>>str1>>str2;
67         Edge[sto[str1]].push_back(sto[str2]);
68         Edge[sto[str2]].push_back(sto[str1]);
69     }
70     int maxstate=(1ll<<n)-1;
71     for(int i=0; i<=maxstate; i++)
72     {
73         dp[i]=cal(i);
74     }
75     for(int i=0; i<=maxstate; i++)
76     {
77         for(int j=i; j; j=(j-1)&i) // 枚举子集
78         {
79             if(dp[i]>dp[i^j]+dp[j])
80             {
81                 dp[i]=dp[i^j]+dp[j];
82                 vis[i]=j;
83             }
84         }
85     }
86     cout<<dp[maxstate]<<endl;
87     dfs(maxstate);
88 }

 

Guess you like

Origin www.cnblogs.com/letlifestop/p/11078136.html