题目链接:http://codeforces.com/contest/208/problem/C

思路:题目要求的是经过1~N的最短路上的某个点的路径数 /  最短路的条数的最大值。一开始我是用spfa得到从1开始的最短路和从N开始的最短路,然后分别从N开始记忆化搜索,得到从1到达最短路径上的u的路径条数,记作dp1[u], 然后再从1开始搜,得到最短路径上从N到达某个点u的路径条数,记作dp2[u],于是经过某个点u的最短路径数目为dp1[u] * dp2[u],然后只需枚举u求最大值即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100 + 10);
int N, M;
long long dp1[MAX_N], dp2[MAX_N];
int vis[MAX_N], dist1[MAX_N], dist2[MAX_N];
vector<int > g[MAX_N]; void spfa(int st, int ed, int *dist)
{
memset(vis, 0, sizeof(vis));
queue<int > que;
que.push(st);
dist[st] = 0;
while (!que.empty()) {
int u = que.front(); que.pop();
vis[u] = 0;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
if (!vis[v]) { vis[v] = 1; que.push(v); }
}
}
}
} long long dfs1(int u, int fa)
{
if (u == 1) return dp1[u] = 1;
if (~dp1[u]) return dp1[u];
long long ans = 0;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (v != fa && dist1[v] + 1 == dist1[u]) {
ans += dfs1(v, u);
}
}
return dp1[u] = ans;
} long long dfs2(int u, int fa)
{
if (u == N) return dp2[u] = 1;
if (~dp2[u]) return dp2[u];
long long ans = 0;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (v != fa && dist2[v] + 1 == dist2[u]) {
ans += dfs2(v, u);
}
}
return dp2[u] = ans;
} int main()
{
while (cin >> N >> M) {
FOR(i, 1, N) g[i].clear();
FOR(i, 1, M) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
memset(dist1, 0x3f, sizeof(dist1));
memset(dist2, 0x3f, sizeof(dist2));
spfa(1, N, dist1);
spfa(N, 1, dist2);
memset(dp1, -1, sizeof(dp1));
memset(dp2, -1, sizeof(dp2));
long long a = dfs1(N, -1);
double ans = 1.0;
dfs2(1, -1);
FOR(i, 1, N) if (dp1[i] >= 1 && dp2[i] >= 1) {
if (i == 1 || i == N) ans = max(ans, 1.0 * dp1[i] * dp2[i] / a);
else ans = max(ans, 2.0 * dp1[i] * dp2[i] / a);
}
printf("%.9f\n", ans);
}
return 0;
}

后来看了别人的做法,发现有更加简单的做法,直接用floyd做就可以了。dist[u][v]表示u到v的最短路径长度,dp[u][v]表示此最短路径长度对应的最短路径数目,然后就是更新的时候注意一下就可以了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100 + 100);
int N, M;
long long dist[MAX_N][MAX_N], dp[MAX_N][MAX_N]; int main()
{
while (cin >> N >> M) {
memset(dist, 0x3f, sizeof(dist));
memset(dp, 0, sizeof(dp));
FOR(i, 1, M) {
int u, v; cin >> u >> v;
dist[u][v] = dist[v][u] = 1;
dp[u][v] = dp[v][u] = 1;
}
FOR(k, 1, N) {
FOR(i, 1, N) {
FOR(j, 1, N) if (dist[i][k] + dist[k][j] <= dist[i][j]) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
dp[i][j] = dp[i][k] * dp[k][j];
}
else {
dp[i][j] += dp[i][k] * dp[k][j];
}
}
}
}
double ans = 1.0;
FOR(i, 2, N - 1) {
if (dist[1][i] + dist[i][N] == dist[1][N]) ans = max(ans, 2.0 * dp[1][i] * dp[i][N]/ dp[1][N]);
}
printf("%.7f\n", ans);
}
return 0;
}

Codeforces Round #130 (Div. 2) C. Police Station的更多相关文章

  1. Codeforces Round #130 (Div. 2) C - Police Station 最短路+dp

    题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 seconds ...

  2. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  3. Codeforces Round #130 (Div. 2)

    A. Dubstep 字符串模拟. string.find()用法 string str; size_t pos = str.find("WUB"); // 返回匹配的第一个位置 ...

  4. Codeforces Round #130 (Div. 2) A. Dubstep

    题目链接: http://codeforces.com/problemset/problem/208/A A. Dubstep time limit per test:2 secondsmemory ...

  5. Codeforces Round #244 (Div. 2) A. Police Recruits

    题目的意思就是找出未能及时处理的犯罪数, #include <iostream> using namespace std; int main(){ int n; cin >> ...

  6. Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)

    传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...

  7. Codeforces Round #580 (Div. 1)

    Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. 【leetcode】Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  2. poj 3268(spfa)

    http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...

  3. [转] Android Volley完全解析(一),初识Volley的基本用法

    版权声明:本文出自郭霖的博客,转载必须注明出处.   目录(?)[-] Volley简介 下载Volley StringRequest的用法 JsonRequest的用法   转载请注明出处:http ...

  4. Divide and conquer:Matrix(POJ 3685)

    矩阵 题目大意:矩阵里面的元素按i*i + 100000 * i + j*j - 100000 * j + i*j填充(i是行,j是列),求最小的M个数 这一题要用到两次二分,实在是二分法的经典,主要 ...

  5. zookeeper集群搭建(windows环境下)

    本次zk测试部署版本为3.4.6版本,下载地址http://mirrors.cnnic.cn/apache/zookeeper/ 限于服务器个数有限本次测试了两种情况 1.单节点方式:部署在一台服务器 ...

  6. List,Set,Map用法以及区别

    List,Set,Map是否继承自Collection接口? 答:List,Set是,Map不是. 如图: Collection ├List │├LinkedList │├ArrayList │└Ve ...

  7. 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  8. Android笔记:四大组件

    1.Activity(是用户可以看到的主要的界面,使用时需要在AndroidManifest.xml中编辑声明.) 启动模式: 启动模式一共有四种,分别是standard.singleTop.sing ...

  9. Mac 下查看 Android Studio 的 SHA1的方法

    cmd -> ->cd .android ->keytool -v -list -keystore debug.keystore 默认口令:android ************* ...

  10. Xcode开发中的6个小技巧

    Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...