UVA 11600 - Masud Rana(状态压缩DP+记忆化搜索)
2014-03-08 11:42
453 查看
E | Masud Rana Input: Standard Input Output: Standard Output |
Masud Rana, A Daring Spy Of Bangladesh Counter Intelligence. He is in a new mission. There is a total n cities in Bangladesh. Each city is connected to all other by bidirectional roads. So there are total n * (n-1) / 2 bidirectional roads. Many of the roads
are under control of evil powers. City a is safely reachable from city b, if there is a path from a to b containing only roads which are not under control of evil powers. There are m roads which are safe from evil powers. The mission of Masud Rana is to destroy
the evil powers of some roads, and make sure that every city is safely reachable from all other.
Masud Rana chose a new strategy for this special mission. Every morning he selects a random city other than the city he stays in at that moment, and visit that city by direct connecting road, in the time of his visit by the road he destroys all evil power
of that road if exists any, and makes that road safe. After reaching new city, he stays there till next morning. In the next morning he checks whether all cities are safely reachable from all others. If he is already done his mission ends, otherwise he repeats
same strategy.
Let us number the cities by 1, 2, ... , n. Masud Rana is in city 1 when he starts his mission.
What is the expected number of days to finish the mission for Masud Rana.
Input
Input will starts with an integer T(T ≤ 100) which denotes the number of test case. Each case starts with two integer N(N ≤ 1 ≤ 30) and M(0 ≤ M ≤ N*(N-1)/2). Each of the next lines contains two integers a and b (1 ≤ a, b ≤ N) which means road connecting
city a and b is safe.
Output
You have to output the expected number of days required for Masud Rana. Print the case number followed by the output. Look at the sample in/out for exact format. Upto 1E-6 error in your output will be acceptable.
2 3 1 2 3 4 1 2 3 | Case 1: 1.0 Case 2: 3.5 |
思路:先把处理把已经可以相互到达的城市作为一个点,状态表示dp[u][s],当期在u这点,已经可以互相到达的城市集合为s,状态转移的方式为。
dp[u][s]时候,已经完成的城市数为have,还没完成的为n - have。那么要选中一个未完成的城市的概率为(n - have) / (n - 1)。也就是平均需要(n - 1) / (n - have)次才能走到未完成的城市,然后在加上s|(1<<i)之后状态需要的平均时间*概率,概率为p[i] * (n - have)。所以状态转移方程为dp[u][s] = (n - 1) / ( n - have) + sum{dp[u][s|(1<<i)] * (p[i] * (n - have))}。
代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
using namespace std;
const int N = 35;
int t, n, m, p
, pn, vis
;
vector<int> g
;
map<int, double> dp
;
int bitcount(int x) {
if (x == 0) return 0;
else return bitcount(x/2) + (x&1);
}
int dfs(int u) {
vis[u] = 1;
int ans = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (vis[v]) continue;
ans += dfs(v);
}
return ans;
}
double DP(int u, int s) {
if (dp[u].count(s)) return dp[u][s];
int have = 0, i;
for (i = 0; i < pn; i++) {
if (s&(1<<i))
have += p[i];
}
if (have == n) return dp[u][s] = 0;
dp[u][s] = (n - 1) * 1.0 / (n - have);
for (i = 0; i < pn; i++) {
if (s&(1<<i)) continue;
dp[u][s] += DP(i, s|(1<<i)) * p[i] / (n - have);
}
return dp[u][s];
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
pn = 0;
memset(vis, 0, sizeof(vis));
memset(g, 0, sizeof(g));
scanf("%d%d", &n, &m);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
dp[pn].clear();
p[pn++] = dfs(i);
}
printf("Case %d: %.6lf\n", ++cas, DP(0, 1));
}
return 0;
}
相关文章推荐
- Android游戏开发---2D游戏中背景的绘制
- 写给明天的软件工程师——(转)
- 快速排序(输出中间过程)
- 世上最无私的爱,父母的爱
- Linux 终端设备的总体简述
- Axis2生成wsdl客户端代码方式
- Xcode真机调试初体验
- IOS应用加入内购或者icloud后HOC无法安装的问题
- Spring IoC之高级特性--加载外部资源
- 区间K大数查询
- Android学习笔记第二弹之事件处理
- python操作MySQL数据库
- Request对象的主要方法
- UVa:1382 Distant Galaxy
- 微信/微博信息整合
- 把一个有序整数数组放到二叉树。
- 第二周上机错误程序——有序的结构体数组(求高人给指点迷津,程序不能运行,快难死俺了)
- Restore IP Addresses - LeetCode
- IOS沙盒相关
- (libgdx小结)粒子编辑器