3890: [Usaco2015 Jan]Meeting Time( dp )

简单的拓扑图dp..

A(i, j), B(i, j) 表示从点 i 长度为 j 的两种路径是否存在. 用bitset就行了

时间复杂度O(m)

----------------------------------------------------------------

#include<bits/stdc++.h>

#define clr(x, c) memset(x, c, sizeof(x))

#define rep(i, n) for(int i = 0; i < n; ++i)

#define foreach(i, x) for(__typeof(x.begin()) i = x.begin(); i != x.end(); i++)

using namespace std;

const int maxn = 109;

struct edge {

int to, a, b;

};

vector<edge> G[maxn];

queue<int> Q;

bitset<10009> A[maxn], B[maxn];

int n, inD[maxn] = {0};

int main() {

freopen("test.in", "r", stdin);

int m;

cin >> n >> m;

while(m--) {

int u, v, a, b;

scanf("%d%d%d%d", &u, &v, &a, &b); u--; v--;

G[u].push_back( (edge) {v, a, b} );

inD[v]++;

}

rep(i, n) {

if(!inD[i]) Q.push(i);

A[i].reset(); B[i].reset();

}

A[0][0] = 1; B[0][0] = 1;

while(!Q.empty()) {

int x = Q.front(); Q.pop();

foreach(it, G[x]) {

A[it->to] |= A[x] << it->a;

B[it->to] |= B[x] << it->b;

if(!--inD[it->to]) Q.push(it->to);

}

}

int ans = -1;

rep(i, 10001) if(A[n - 1][i] && B[n - 1][i]) {

ans = i;

break;

}

if(!~ans)

puts("IMPOSSIBLE");

else

printf("%d\n", ans);

return 0;

}

----------------------------------------------------------------

3890: [Usaco2015 Jan]Meeting Time

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 95  Solved: 65
[Submit][Status][Discuss]

Description

Bessie and her sister Elsie want to travel from the barn to their favorite field, such that they leave at exactly the same time from the barn, and also arrive at exactly the same time at their favorite field. The farm is a collection of N fields (1 <= N <= 100) numbered 1..N, where field 1 contains the barn and field N is the favorite field. The farm is built on the side of a hill, with field X being higher in elevation than field Y if X < Y. An assortment of M paths connect pairs of fields. However, since each path is rather steep, it can only be followed in a downhill direction. For example, a path connecting field 5 with field 8 could be followed in the 5 -> 8 direction but not the other way, since this would be uphill. Each pair of fields is connected by at most one path, so M <= N(N-1)/2. It might take Bessie and Elsie different amounts of time to follow a path; for example, Bessie might take 10 units of time, and Elsie 20. Moreover, Bessie and Elsie only consume time when traveling on paths between fields -- since they are in a hurry, they always travel through a field in essentially zero time, never waiting around anywhere. Please help determine the shortest amount of time Bessie and Elsie must take in order to reach their favorite field at exactly the same moment.
给出一个n个点m条边的有向无环图,每条边两个边权。 
n<=100,没有重边。 
然后要求两条长度相同且尽量短的路径, 
路径1采用第一种边权,路径2采用第二种边权。 
没有则输出”IMPOSSIBLE”

Input

The first input line contains N and M, separated by a space. Each of the following M lines describes a path using four integers A B C D, where A and B (with A < B) are the fields connected by the path, C is the time required for Bessie to follow the path, and D is the time required for Elsie to follow the path. Both C and D are in the range 1..100.

Output

A single integer, giving the minimum time required for Bessie and Elsie to travel to their favorite field and arrive at the same moment. If this is impossible, or if there is no way for Bessie or Elsie to reach the favorite field at all, output the word IMPOSSIBLE on a single line.

Sample Input

3 3
1 3 1 2
1 2 1 2
2 3 1 2

Sample Output

2

SOLUTION NOTES:

Bessie is twice as fast as Elsie on each path, but if Bessie takes the
path 1->2->3 and Elsie takes the path 1->3 they will arrive at the
same time.

HINT

Source

Silver

时间: 2024-12-20 16:03:56

3890: [Usaco2015 Jan]Meeting Time( dp )的相关文章

BZOJ 3890 Usaco2015 Jan Meeting Time 拓扑图DP

题目大意 题上的中文题意太不明确了... 给出一个拓扑图,每条有向边有两个权值,有两个人从1出发到n,分别走这两种权值.问有没有权值使得这两个人都能走过这些权值到达n. 思路 看懂了题之后就水了.维护两个数组表示从1号节点是否能够通过i的权值到达j.然后做拓扑图DP. CODE #define _CRT_SECURE_NO_WARNINGS #include <queue> #include <cstdio> #include <cstring> #include &l

bzoj3886: [Usaco2015 Jan]Moovie Mooving

题意: PoPoQQQ要在电影院里呆L分钟,这段时间他要看小型电影度过.电影一共N部,每部都播放于若干段可能重叠的区间,PoPoQQQ决不会看同一部电影两次.现在问他要看最少几部电影才能度过这段时间? 注:必须看电影才能在电影院里呆着,同时一场电影可以在其播放区间内任意时间入场出场.N=20.每部电影的重复区间<=100. =>N=20.那么我们还是考虑二进制枚举.转移方程类似.时间复杂度类似.哎呀套路啊... #include<cstdio> #include<cstrin

BZOJ 3887[Usaco2015 Jan]Grass Cownoisseur

题面: 3887: [Usaco2015 Jan]Grass Cownoisseur Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 130[Submit][Status][Discuss] Description In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow pat

bzoj3887: [Usaco2015 Jan]Grass Cownoisseur

题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们先考虑缩点.然后观察缩点后的图可以发现新的路径中必定只有一条边是反向的才符合条件.那么我们可以联想到某道最短路的题将边反向存一遍后分别从s和t跑一跑.那么这里bfs跑一跑就行了.然后有一个坑点:这种重建图的注意es和edges不然es会在中途就被修改掉了... #include<cstdio> #

线段树 BZOJ3888 [Usaco2015 Jan]Stampede

3888: [Usaco2015 Jan]Stampede Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 253  Solved: 81[Submit][Status][Discuss] Description Farmer John's N cows (1 <= N <= 50,000) appear to be stampeding along the road at the front of FJ's farm, but they are

【BZOJ3886】【Usaco2015 Jan】Moovie Mooving 状态压缩 动态规划

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44040735"); } 题意: PoPoQQQ要在电影院里呆L分钟,这段时间他要看小型电影度过.电影一共N部,每部都播放于若干段可能重叠的区间,PoPoQQQ决不会看同一部电影两次.现在问他要看最少几部电影才能度过这段时间? 注:必

BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )

完全背包.. --------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define

【BZOJ3888】【Usaco2015 Jan】Stampede 线段树判区间覆盖

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44066313"); } 题意: PoPoQQQ站在原点上向y轴正半轴看,然后有一群羊驼从他眼前飞过.这些羊驼初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走一个单位. PoPoQQQ能看见一匹羊驼当且仅

BZOJ3888 [Usaco2015 Jan]Stampede

我们只要把每头牛开始遮挡视线和结束遮挡视线的时间点都搞出来就好= = 再按照y轴排序...然后变成线段覆盖了..线段树搞一下就好了? 1 /************************************************************** 2 Problem: 3888 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:1204 ms 7 Memory:22684 kb 8 ****************