Light OJ 1406 Assassin's Creed state compression DP + strongly connected contraction point + minimum path coverage

Source: Internet
Author: User

Source: Light OJ 1406 Assassin's Creed

Directed Graph sends the least people to all cities and no one can walk anywhere else.

Ideas: the minimum number of people who can finish the whole graph is obviously the problem of least path coverage. Here there may be loops, so we need to shrink the point. However, we can see that a strongly connected component may have to be split into N at most 15, so the state is compressed.

Divide the full graph into sub-States. Each sub-state is scaled down to obtain the minimum path coverage. This solves the issue of splitting strongly connected components. Finally, the state is compressed and the DP is used to solve the optimal value.

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <stack>using namespace std;const int maxn = 16;vector <int> G[maxn], G2[maxn];int dp[1<<maxn];int pre[maxn], low[maxn], sccno[maxn];int clock, scc_cnt;int n, m;stack <int> S;int a[maxn][maxn];int b[maxn][maxn];void dfs(int u, int x){pre[u] = low[u] = ++clock;S.push(u);for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(!(x&(1<<v)))continue;if(!pre[v]){dfs(v, x);low[u] = min(low[u], low[v]); }else if(!sccno[v]){low[u] = min(low[u], pre[v]);}}if(pre[u] == low[u]){scc_cnt++;while(1){int x = S.top(); S.pop();sccno[x] = scc_cnt;if(x == u)break;}}}int find_scc(int x){memset(sccno, 0, sizeof(sccno));memset(pre, 0, sizeof(pre));scc_cnt = 0, clock = 0;for(int i = 0; i < n; i++){if(x&(1<<i) && !pre[i])dfs(i, x);}return scc_cnt;}int y[maxn];bool vis[maxn];bool xyl(int u){for(int i = 0; i < G2[u].size(); i++){int v = G2[u][i];if(vis[v])continue;vis[v] = true;if(y[v] == -1 || xyl(y[v])){y[v] = u;return true;}}return false;}int match(){int ans = 0;memset(y, -1, sizeof(y));for(int i = 1; i <= scc_cnt; i++){memset(vis, false, sizeof(vis));if(xyl(i))ans++;}return scc_cnt-ans;}int main(){int cas = 1;int T;scanf("%d", &T);while(T--){scanf("%d %d", &n, &m);for(int i = 0; i < n; i++)G[i].clear();memset(a, 0, sizeof(a));while(m--){int u, v;scanf("%d %d", &u, &v);u--;v--;G[u].push_back(v);a[u][v] = 1;}dp[0] = 0;//puts("sdf");for(int i = 1; i < (1<<n); i++){//memset(b, 0, sizeof(b));find_scc(i);for(int j = 0; j <= n; j++)G2[j].clear();for(int j = 0; j < n; j++)for(int k = 0; k < n; k++)if(a[j][k] && sccno[j] && sccno[k] && sccno[j] != sccno[k])G2[sccno[j]].push_back(sccno[k]);dp[i] = match();}//puts("sdf");for(int s = 1; s < (1<<n); s++){for(int i = s; i; i = s&(i-1)){dp[s] = min(dp[s], dp[s^i] + dp[i]);}}printf("Case %d: %d\n", cas++, dp[(1<<n)-1]);}return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.