HDU4511-小明系列故事――女友的考验
2017-11-13 15:04
281 查看
小明系列故事——女友的考验
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768K (Java/Others)
Total Submission(s): 1847 Accepted Submission(s): 491
Problem Description
终于放寒假了,小明要和女朋友一起去看电影。这天,女朋友想给小明一个考验,在小明正准备出发的时候,女朋友告诉他,她在电影院等他,小明过来的路线必须满足给定的规则:
1、假设小明在的位置是1号点,女朋友在的位置是n号点,则他们之间有n-2个点可以走,小明每次走的时候只能走到比当前所在点编号大的位置;
2、小明来的时候不能按一定的顺序经过某些地方。比如,如果女朋友告诉小明不能经过1 -> 2 -> 3,那么就要求小明来的时候走过的路径不能包含有1 -> 2 -> 3这部分,但是1 -> 3 或者1 -> 2都是可以的,这样的限制路径可能有多条。
这让小明非常头痛,现在他把问题交给了你。
特别说明,如果1 2 3这三个点共线,但是小明是直接从1到3然后再从3继续,那么此种情况是不认为小明经过了2这个点的。
现在,小明即想走最短的路尽快见到女朋友,又不想打破女朋友的规定,你能帮助小明解决这个问题吗?
Input
输入包含多组样例,每组样例首先包含两个整数n和m,其中n代表有n个点,小明在1号点,女朋友在n号点,m代表小明的女朋友有m个要求;
接下来n行每行输入2个整数x 和y(x和y均在int范围),代表这n个点的位置(点的编号从1到n);
再接着是m个要求,每个要求2行,首先一行是一个k,表示这个要求和k个点有关,然后是顺序给出的k个点编号,代表小明不能走k1 -> k2 -> k3 ……-> ki这个顺序的路径;
n 和 m等于0的时候输入结束。
[Technical Specification]
2 <= n <= 50
1 <= m <= 100
2 <= k <= 5
Output
对于每个样例,如果存在满足要求的最短路径,请输出这个最短路径,结果保留两位小数;否则,请输出”Can not be reached!” (引号不用输出)。
Sample Input
3 1
1 1
2 1
3 1
2
1 2
2 1
0 0
1 1
2
1 2
5 3
0 0
5 3
1 2
1 22
5 21
3
1 2 3
2
4 5
2
1 5
0 0
Sample Output
2.00
Can not be reached!
21.65
Source
2013腾讯编程马拉松初赛第二场(3月22日)
Recommend
liuyiding
解题思路:ac自动机
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cctype> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <bitset> #include <functional> using namespace std; #define LL long long const double INF = 1e30; int n, m, a[10]; pair<int, int>p[55]; double dp[55][1200]; double dis(int x, int y) { return sqrt((1.0 * p[x].first - p[y].first)*(1.0 * p[x].first - p[y].first) + (1.0 * p[x].second - p[y].second)*(1.0 * p[x].second - p[y].second)); } struct Trie { int next[1200][55], fail[1200], flag[1200]; int root, tot; int newnode() { for (int i = 1; i <= n; i++) next[tot][i] = -1; flag[tot++] = 0; return tot - 1; } void init() { tot = 0; root = newnode(); } void insert(int a[], int x) { int k = root; for (int i = 0; i < x; i++) { if (next[k][a[i]] == -1) next[k][a[i]] = newnode(); k = next[k][a[i]]; } flag[k] = 1; } void build() { queue<int>q; fail[root] = root; for (int i = 1; i <= n; i++) { if (next[root][i] == -1) next[root][i] = root; else { fail[next[root][i]] = root; q.push(next[root][i]); } } while (!q.empty()) { int pre = q.front(); q.pop(); if (flag[fail[pre]]) flag[pre] = 1; for (int i = 1; i <= n; i++) { if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i]; else { fail[next[pre][i]] = next[fail[pre]][i]; q.push(next[pre][i]); } } } } void solve() { for (int i = 1; i <= n; i++) for (int j = 0; j < tot; j++) dp[i][j] = INF; dp[1][next[root][1]] = 0; for (int i = 1; i < n; i++) for (int j = 0; j < tot; j++) if (dp[i][j] < INF) { for (int k = i + 1; k <= n; k++) { if (flag[next[j][k]]) continue; dp[k][next[j][k]] = min(dp[k][next[j][k]], dp[i][j] + dis(i, k)); } } double ans = INF; for (int i = 0; i < tot; i++) ans = min(ans, dp [i]); if (ans == INF) printf("Can not be reached!\n"); else printf("%.2f\n", ans); } void debug() { for (int i = 0; i < tot; i++) { printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]); for (int j = 0; j < 26; j++) printf("%2d", next[i][j]); printf("]\n"); } } }ac; int main() { while (~scanf("%d %d", &n, &m) && (n + m)) { for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].first, &p[i].second); ac.init(); for (int i = 1; i <= m; i++) { int k; scanf("%d", &k); for (int j = 0; j < k; j++) scanf("%d", &a[j]); ac.insert(a, k); } ac.build(); ac.solve(); } return 0; }
相关文章推荐
- hdu4511---小明系列故事——女友的考验(AC自动机+dp)
- hdu4511 小明系列故事——女友的考验(AC自动机+dp)
- hdu4511小明系列故事——女友的考验(ac自动机+最短路)
- HDU4511 小明系列故事——女友的考验(AC自动机 + DP)
- HDU 4511 小明系列故事——女友的考验(自动机+DP)
- hdu 4511 小明系列故事——女友的考验(AC自动机+dp)
- hdu - 4511 小明系列故事——女友的考验(AC自动机+DP)
- HDU 4511 小明系列故事——女友的考验(AC自动机 + DP)
- hdu 4511 小明系列故事——女友的考验
- 小明系列故事——女友的考验
- HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)
- HDU 4511 小明系列故事——女友的考验 (AC自动机 + dp)
- hdu 4511 小明系列故事——女友的考验
- 【HDU】4511 小明系列故事——女友的考验 (AC自动机+DP)
- HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
- HDU - 4511 小明系列故事――女友的考验(AC自动机+DP)
- hdu 4511 小明系列故事——女友的考验
- HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
- HDU-4511 小明系列故事——女友的考验 floyd变种-标号递增最短路
- AC自动机 + 二维最短路 HDU 4511 小明系列故事――女友的考验