Codeforces 685E Travelling Through the Snow Queen's Kingdom(DP)
2017-02-02 21:37
381 查看
题意:
给你一张图,每一条边都有一个编号i,经过每条边的时间为1,如果当你到达这条边的时间小于i的话,就必须等到i才能走出这条边,如果大于i,就走不出去了,也就是不通了。然后给你一些询问l, r, s, t,问你是否可以从s出发,时间为l,在r时间之前到达s。
解法:
我们假设动态规划的状态为dp[s][t]表示从s到t要花费的最少时间,我们考虑倒叙添加边,这样的话每当新添加一条边(u,v),只会增加经过这条边的路,这个需要自己思考一下才能理解。然后对于每一个点都来更新dp值。所有的询问也按照l倒叙处理,每次增加一条边,就处理一些能够处理的询问。
给你一张图,每一条边都有一个编号i,经过每条边的时间为1,如果当你到达这条边的时间小于i的话,就必须等到i才能走出这条边,如果大于i,就走不出去了,也就是不通了。然后给你一些询问l, r, s, t,问你是否可以从s出发,时间为l,在r时间之前到达s。
解法:
我们假设动态规划的状态为dp[s][t]表示从s到t要花费的最少时间,我们考虑倒叙添加边,这样的话每当新添加一条边(u,v),只会增加经过这条边的路,这个需要自己思考一下才能理解。然后对于每一个点都来更新dp值。所有的询问也按照l倒叙处理,每次增加一条边,就处理一些能够处理的询问。
// Created by CQU_CST_WuErli // Copyright (c) 2016 CQU_CST_WuErli. All rights reserved. // //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <string> #include <vector> #include <map> #include <queue> #include <stack> #include <set> #include <algorithm> #include <sstream> #define CLR(x) memset(x,0,sizeof(x)) #define OFF(x) memset(x,-1,sizeof(x)) #define MEM(x,a) memset((x),(a),sizeof(x)) #define BUG cout << "I am here" << endl #define lookln(x) cout << #x << "=" << x << endl #define SI(a) scanf("%d", &a) #define SII(a,b) scanf("%d%d", &a, &b) #define SIII(a,b,c) scanf("%d%d%d", &a, &b, &c) const int INF_INT=0x3f3f3f3f; const long long INF_LL=0x7f7f7f7f; const int MOD=1e9+7; const double eps=1e-10; const double pi=acos(-1); typedef long long ll; using namespace std; const int N = 200200; int n, m, q; int u , v ; struct Query { int l, r, s, t, id; Query(int l, int r, int s, int t, int id):l(l), r(r), s(s), t(t), id(id) {} Query() {} bool operator < (const Query& rhs) const { return l > rhs.l; } }; vector<Query> qs; int dp[1010][1010]; int ans ; int main(int argc, char const *argv[]) { #ifdef LOCAL freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout); #endif SIII(n, m, q); for (int i = 1; i <= m; i++) SII(u[i], v[i]); for (int i = 1; i <= q; i++) { int l, r, s, t; SII(l, r); SII(s, t); qs.emplace_back(l, r, s, t, i); } sort(qs.begin(), qs.end()); MEM(dp, INF_INT); for (int i = 1; i <= n; i++) dp[i][i] = 0; int cnt = 0; for (int i = m; i >= 1; i--) { dp[u[i]][v[i]] = dp[v[i]][u[i]] = i; for (int j = 1; j <= n; j++) { if (j != u[i]) dp[v[i]][j] = min(dp[v[i]][j], dp[u[i]][j]); if (j != v[i]) dp[u[i]][j] = min(dp[u[i]][j], dp[v[i]][j]); } while (cnt < qs.size() && qs[cnt].l >= i) { ans[qs[cnt].id] = dp[qs[cnt].s][qs[cnt].t] <= qs[cnt].r; cnt++; } } for (int i = 1; i <= q; i++) puts(ans[i] ? "Yes" : "No"); return 0; }
相关文章推荐
- CodeForces 149 D.Coloring Brackets(区间DP)
- CodeForces - 391B Word Folding(题目超难题解超简单的DP)
- CodeForces 55D 数位DP 能被它自身数位上的所有数整除
- Codeforces 437E The Child and Polygon(区间DP)
- CodeForces 711C Coloring Trees (DP)
- codeforces 417D 状压DP
- Codeforces 666C C. Codeword 组合数学+DP
- 【dp】【codeforces】 451D Count Good Substrings
- CodeForces 219D-Choosing Capital for Treeland(树形dp)
- Codeforces 167B Wizards and Huge Prize 概率dp(水
- Codeforces 38H The Great Marathon [dp]
- Codeforces 722E [DP]
- Codeforces 597C Subsequences 【树状数组优化DP】
- Codeforces 327A Flipping Game(区间DP,暴力枚举)
- [杂题 背包DP] Codeforces 793E Tinkoff Challenge - Elimination Round E. Problem of offices
- Codeforces 431C —— k-Tree(DP)
- codeforces 855C 树DP
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- [CodeForces 279C] Ladder (DP)
- codeforces 258div2 A Game With Sticks(DP)