简单的拓扑图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

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

  1. BZOJ 3890 [Usaco2015 Jan]Meeting Time:拓扑图dp

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3890 题意: 给你一个有向图,n个点(n <= 100),m条边. 且所有的边都是从 ...

  2. bzoj3890 [Usaco2015 Jan]Meeting Time

    Description Bessie and her sister Elsie want to travel from the barn to their favorite field, such t ...

  3. BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*

    BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur Description In an effort to better manage the grazing pat ...

  4. BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP

    BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP Description In an effort to better manage t ...

  5. [bzoj3887][Usaco2015 Jan]Grass Cownoisseur_trajan_拓扑排序_拓扑序dp

    [Usaco2015 Jan]Grass Cownoisseur 题目大意:给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在 ...

  6. [补档][Usaco2015 Jan]Grass Cownoisseur

    [Usaco2015 Jan]Grass Cownoisseur 题目 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过? (一个点在路 ...

  7. bzoj3887: [Usaco2015 Jan]Grass Cownoisseur

    题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们 ...

  8. 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分

    题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...

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

    完全背包.. --------------------------------------------------------------------------------------- #incl ...

随机推荐

  1. ASP.NET PipeLine #Reprinted#

    从ASP.NET 1.0 起,相比于ASP中的COM, PipeLine 就是一项重大的改进. ASP.NET 时代的管道模型究竟是怎么样的? 我们能接触到的四个最重要的概念就是HttpApplica ...

  2. hibernate 数据关联一对多 3.1

    一对多,多对一 (在多的一端存放一的外键) 但是在实体类中不需要创建这个外键 // 在一的一方创建Set集合 public class User { private Integer id; priva ...

  3. [WPF疑难] 继承自定义窗口

    原文 [WPF疑难] 继承自定义窗口 [WPF疑难] 继承自定义窗口 周银辉 项目中有不少的弹出窗口,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于Window自身的,但每个弹出框 ...

  4. Phalcon框架中的另类使用

    不像传统的PHP框架,假设框架想被还有一个框架使用仅仅能通过rpc或是引入文件等的方式.Phalcon能够在其他框架中直接使用.这是因为Phalcon是以扩展的形式存在的,在server载入时会直接载 ...

  5. H264格式具体说明

    一 H.264句法1.1元素分层结构H.264编码器输出的Bit流中,每一个Bit都隶属于某个句法元素.句法元素被组织成有层次的结构,分别描写叙述各个 一 H.264句法 1.1元素分层结构 H.26 ...

  6. ThinkPHP - 进行继承时的 构造函数

    被继承文件:PublicController.class.php <?php namespace Admin\Controller; use Think\Controller; class Pu ...

  7. c语言求最大公约数和最小公倍数

    求最大公约数和最小公倍数 假设有两个数a和b,求a,b的最大公约数和最小公倍数实际上是一个问题,得出这两个数的最大公约数就可以算出它们的最小公倍数. 最小公倍数的公式是 a*b/m m为最大公约数 因 ...

  8. (Problem 17)Number letter counts

    If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...

  9. Hibernate JPA 中配置Ehcache二级缓存

    在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错.具体过程如下: 1, 需要引入的jar包 http://ehcache.org/downloads/catalog 下载的包 ...

  10. Android的BUG(二) - SurfaceTexture中的野指针

    当初遇到这个bug,是不定期的低概率出现,最后找到一个比较容易重现的步骤: 启动系统 然后进google +  新建一个帐号(注意是新建一个帐号)  没几步就重启了 这个BUG,一开始追踪也是无头绪的 ...