Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short.

Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range 1..2,000,000,000.

Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000).

Worse still, Moo U only has classrooms for an odd number N (1 <= N <= 19,999) of the C (N <= C <= 100,000) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible.

Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 and exactly two values below it.

Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves.

* Line 1: Three space-separated integers N, C, and F

* Lines 2..C+1: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs

* Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1.

1 | 3 5 70 |

1 | 35 |

**Sample output:** If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70.

1 | `#include <cstdio>` |

- 从申请者里录取n个，n为奇数，要求他们需要的开销和不超过预算，求被录取者的得分最大可能的中位数。
- 从大到小遍历中位数的可能，利用优先队列维护左右两侧的最小开销。

To avoid unsightly burns while tanning, each of the *C* (1 ≤ *C* ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow *i* has a minimum and maximum *SPF* rating (1 ≤ *minSPFi* ≤ 1,000; *minSPFi* ≤ *maxSPFi* ≤ 1,000) that will work. If the *SPF* rating is too low, the cow suffers sunburn; if the *SPF* rating is too high, the cow doesn't tan at all........

The cows have a picnic basket with *L* (1 ≤ *L* ≤ 2500) bottles of sunscreen lotion, each bottle *i* with an *SPF* rating *SPFi* (1 ≤ *SPFi* ≤ 1,000). Lotion bottle *i* can cover *coveri* cows with lotion. A cow may lotion from only one bottle.

What is the maximum number of cows that can protect themselves while tanning given the available lotions?

* Line 1: Two space-separated integers: *C* and *L* * Lines 2..*C*+1: Line *i* describes cow *i*'s lotion requires with two integers: *minSPFi* and *maxSPFi* * Lines *C*+2..*C*+*L*+1: Line *i*+*C*+1 describes a sunscreen lotion bottle *i* with space-separated integers: *SPFi* and *coveri*

A single line with an integer that is the maximum number of cows that can be protected while tanning

1 | 3 2 |

1 | 2 |

1 | `#include <cstdio> ` |

- 每只奶牛有自己的有效防晒区间，给出多种不同数量的防晒霜，求最多可以让多少只奶牛成功防晒。
- 将区间按左端点升序排序，将防晒霜按防晒指数升序排序。对于每种防晒霜，将区间左端点不超过它的牛加入最小堆，每次从堆头贪心。

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch. You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

For each test case output the answer on a single line.

1 | 3 10 |

1 | 8 |

1 | `#include <cstdio>` |

- n 个数 a_1,a_2,...a_n，每个数有 c_1,c_2,...,c_n 个，求组和成为不大于 m 的整数个数。
- DP，dp[i][j] 代表前 i 个数组成数字 j，数字 i 的最少使用个数，初始值为 -1，代表前 i 个数不能组成数字 j，若 d[i-1][j] > -1，d[i][j] = 0，否则 d[i][j] = d[i][j-a[i]]+1。
- dp 数组只与前项有关，可以滚动压缩 dp。

Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that many of the ants were siblings, indistinguishable from one another. She also realized the sometimes only one ant would go for food, sometimes a few, and sometimes all of them. This made for a large number of different sets of ants!

Being a bit mathematical, Bessie started wondering. Bessie noted that the hive has T (1 <= T <= 1,000) families of ants which she labeled 1..T (A ants altogether). Each family had some number Ni (1 <= Ni <= 100) of ants.

How many groups of sizes S, S+1, ..., B (1 <= S <= B <= A) can be formed?

While observing one group, the set of three ant families was seen as {1, 1, 2, 2, 3}, though rarely in that order. The possible sets of marching ants were:

3 sets with 1 ant: {1} {2} {3} 5 sets with 2 ants: {1,1} {1,2} {1,3} {2,2} {2,3} 5 sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3} 3 sets with 4 ants: {1,2,2,3} {1,1,2,2} {1,1,2,3} 1 set with 5 ants: {1,1,2,2,3}

Your job is to count the number of possible sets of ants given the data above.

* Line 1: 4 space-separated integers: T, A, S, and B

* Lines 2..A+1: Each line contains a single integer that is an ant type present in the hive

* Line 1: The number of sets of size S..B (inclusive) that can be created. A set like {1,2} is the same as the set {2,1} and should not be double-counted. Print only the LAST SIX DIGITS of this number, with no leading zeroes or spaces.

1 | 3 5 2 3 |

1 | 10 |

INPUT DETAILS:

Three types of ants (1..3); 5 ants altogether. How many sets of size 2 or size 3 can be made?

OUTPUT DETAILS:

5 sets of ants with two members; 5 more sets of ants with three members

1 | `#include <cstdio>` |

- t 个家族，每个家族有不同的人数，求 t 个家族中选取 s,s+1,...,b 个人有多少种组合。
- \[dp[i][j] = \sum^{k = min(num[i],j)}_{k=0} dp[i-1][j-k]\]
- dp[i][j] 代表从前 i 个家族中选出 j 个人，有多少种组合。

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:

1 | 1 @ US$3 + 1 @ US$2 |

Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of \(1..\)K (1 <= K <= 100).

A single line with two space-separated integers: N and K.

A single line with a single integer that is the number of unique ways FJ can spend his money.

1 | 5 3 |

1 | 5 |

1 | `#include <cstdio> ` |

- 农夫约翰有N元钱，市场上有价值1……K的商品无限个，求所有的花钱方案？
- 完全背包 + 大数相加

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.

Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).

Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.

* Line 1: Two space separated integers: T and W

* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

1 | 7 2 |

1 | 6 |

INPUT DETAILS:

Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.

OUTPUT DETAILS:

Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.

1 | `#include <cstdio>` |

- 苹果每分钟从两棵树的其中一棵落下，在树1和树2间可以跳换给定次数，问最多可以接到多少苹果，初始在树1。
- d[i][j]表示到达i时刻，已换树j次，位于j%2+1号树下。转移d[i][j]=max(d[i-1][j],d[i-1][j-1])+((j&1)+1==a[i])。

Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name to apply for an international patent). The stripies are transparent amorphous amebiform creatures that live in flat colonies in a jelly-like nutrient medium. Most of the time the stripies are moving. When two of them collide a new stripie appears instead of them. Long observations made by our scientists enabled them to establish that the weight of the new stripie isn't equal to the sum of weights of two disappeared stripies that collided; nevertheless, they soon learned that when two stripies of weights m1 and m2 collide the weight of resulting stripie equals to 2*sqrt(m1*m2). Our chemical biologists are very anxious to know to what limits can decrease the total weight of a given colony of stripies. You are to write a program that will help them to answer this question. You may assume that 3 or more stipies never collide together.

The first line of the input contains one integer N (1 <= N <= 100) - the number of stripies in a colony. Each of next N lines contains one integer ranging from 1 to 10000 - the weight of the corresponding stripie.

The output must contain one line with the minimal possible total weight of colony with the accuracy of three decimal digits after the point.

1 | 3 |

1 | 120.000 |

1 | `#include <cstdio>` |

- 哈夫曼编码，从大到小开始合并，因为每次开根号，大数开的根号越多，其变得越小。

The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) cents to produce one unit of yogurt in week i. Yucky's factory, being well-designed, can produce arbitrarily many units of yogurt each week.

Yucky Yogurt owns a warehouse that can store unused yogurt at a constant fee of S (1 <= S <= 100) cents per unit of yogurt per week. Fortuitously, yogurt does not spoil. Yucky Yogurt's warehouse is enormous, so it can hold arbitrarily many units of yogurt.

Yucky wants to find a way to make weekly deliveries of Y_i (0 <= Y_i <= 10,000) units of yogurt to its clientele (Y_i is the delivery quantity in week i). Help Yucky minimize its costs over the entire N-week period. Yogurt produced in week i, as well as any yogurt already in storage, can be used to meet Yucky's demand for that week.

* Line 1: Two space-separated integers, N and S.

* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.

* Line 1: Line 1 contains a single integer: the minimum total cost to satisfy the yogurt schedule. Note that the total might be too large for a 32-bit integer.

1 | 4 5 |

1 | 126900 |

OUTPUT DETAILS: In week 1, produce 200 units of yogurt and deliver all of it. In week 2, produce 700 units: deliver 400 units while storing 300 units. In week 3, deliver the 300 units that were stored. In week 4, produce and deliver 500 units.

1 | `#include <cstdio>` |

- 每周产奶的单位成本是 c，需求量是 y，固定存储成本是 5，求最少成本完成几周的任务。
- 拿第 i+1 周的成本减去 i * 5，就是模拟地其在第一周的成本，这样将每一周的成本都降维到第一周，选取前 i 周最小的那个作为生产之后的成本。

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

- The minimum number of stalls required in the barn so that each cow can have her private milking period
- An assignment of cows to these stalls over time

Many answers are correct for each test dataset; a program will grade your answer.

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Line 1: The minimum number of stalls the barn must have.

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

1 | 5 |

1 | 4 |

Explanation of the sample:

Other outputs using the same number of stalls are possible.

1 | Time 1 2 3 4 5 6 7 8 9 10 |

Other outputs using the same number of stalls are possible.

1 | `#include <cstdio>` |

- 每头奶牛有自己的产奶时间区间，现在要建一批牛栏，使得每个牛都能独自在牛栏里产奶，求最少牛栏数和牛与牛栏的分配关系。
- 将牛按开始时间排序，最小堆维护每个牛栏最右端点的值，对每个牛，若其开始时间大于堆顶，则放入此牛栏，否则新建牛栏。

The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes.

They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited).

With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201).

Determine the count of the number of distinct integers that can be created in this manner.

* Lines 1..5: The grid, five integers per line

* Line 1: The number of distinct integers that can be constructed

1 | 1 1 1 1 1 |

1 | 15 |

OUTPUT DETAILS: 111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.

1 | `#include <cstdio> ` |

- 在 5 * 5 的矩阵中，任一点出发，走出长度为 6 的序列，可以重复走，求出最后长度为 6 的不同序列的个数。
- dfs 遍历搜索。
- 每次序列存储为一个字符串，或者看成一个 6 位数，符合条件放入集合中去重即可。

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that *M* meteors (1 ≤ *M* ≤ 50,000) will strike, with meteor *i* will striking point (*Xi*, *Yi*) (0 ≤ *Xi* ≤ 300; 0 ≤ *Yi* ≤ 300) at time *Ti* (0 ≤ *Ti* ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

* Line 1: A single integer: *M* * Lines 2..*M*+1: Line *i*+1 contains three space-separated integers: *Xi*, *Yi*, and *Ti*

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

1 | 4 |

1 | 5 |

1 | `#include <cstdio>` |

- 给出M次流星的落点和时间，流星会摧毁该点和上下左右四点，矩阵内每点只在摧毁前才可到达。从左上角出发，求能够到达永不会被摧毁的点最短时间。
- 预处理出每点最早被摧毁的时间，使用边界和摧毁时间作入队限制，直到队列空或到达不会被摧毁的点。
- 注意初始状态可能为被摧毁或永不摧毁。
- https://github.com/yogykwan/acm-challenge-workbook/blob/master/src/poj3669.cpp
- 题目给的范围是 300，用 301 作为边界反而会出错。。。

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile

'#' - a red tile

'@' - a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

1 | 6 9 |

1 | 45 |

1 | `#include <cstdio>` |

- 二维矩阵中，有 '@'，'.'，'#'，三种字符，其中 @ 是起点，# 是红色，. 是黑色，求@连通的最大最色格子数目。@ 本身是黑色。
- dfs 连通图。

You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+...+xnyn.

Suppose you are allowed to permute the coordinates of each vector as you wish. Choose two permutations such that the scalar product of your two new vectors is the smallest possible, and output that minimum scalar product.

The first line of the input file contains integer number **T** - the number of test cases. For each test case, the first line contains integer number **n**. The next two lines contain **n** integers each, giving the coordinates of v1 and v2 respectively.

For each test case, output a line

1 | Case #X: Y |

where **X** is the test case number, starting from 1, and **Y** is the minimum scalar product of all permutations of the two given vectors.

Small dataset

T= 1000 1 ≤n≤ 8 -1000 ≤xi,yi ≤ 1000

Large dataset

T= 10 100 ≤n≤ 800 -100000 ≤xi,yi ≤ 100000

1 | Input: |

1 | `#include <cstdio>` |

- 排序不等式：反序和 <= 乱序和 <= 顺序和
- 数学归纳法证明

Windy has a country, and he wants to build an army to protect his country. He has picked up *N* girls and *M* boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl **x** and boy **y** have a relationship *d* and one of them has been collected, Windy can collect the other one with 10000-*d* RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

The first line of input is the number of test case. The first line of each test case contains three integers, *N*, *M* and *R*. Then *R* lines followed, each contains three integers *xi*, *yi* and *di*. There is a blank line before each test case.

1 ≤ *N*, *M* ≤ 10000 0 ≤ *R* ≤ 50,000 0 ≤ *xi* < *N* 0 ≤ *yi* < *M* 0 < *di* < 10000

For each test case output the answer in a single line.

1 | 2 |

1 | 71071 |

1 | `#include <cstdio>` |

- 花费费用最少，则选取全部顶点花费的代价最大。即最大生成树？将边权重全部取反，即是最小生成树。
- kruskal 求最小生成树。
- https://blog.csdn.net/u013480600/article/details/37996747

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of *R* (1 ≤ *R* ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ *N* ≤ 5000) intersections, conveniently numbered 1..*N*. Bessie starts at intersection 1, and her friend (the destination) is at intersection *N*.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Line 1: Two space-separated integers: *N* and *R* Lines 2..*R*+1: Each line contains three space-separated integers: *A*, *B*, and *D* that describe a road that connects intersections *A* and *B* and has length *D* (1 ≤ *D* ≤ 5000)

Line 1: The length of the second shortest path between node 1 and node *N*

1 | 4 4 |

1 | 450 |

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

1 | `#include <cstdio>` |

- 求解最短路径用 Dijkstra 算法。
- a 到 b 的次短路径 = min {a到c的最短路径+c到b的次短路径，a到c的次短路径+c到b的最短路径}。

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

* Line 1: A single integer, N

* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.

* Line N+2: Two space-separated integers, L and P

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

1 | 4 |

1 | 2 |

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

1 | `#include <cstdio>` |

- 输入的加油站距离是从城镇（终点）到加油站，并非是卡车所在位置（起点）。
- 输入的每个加油站并非是排好序的，需自己按距离排序。
- 可以看作，卡车可以在任意时间，加上已经经过的加油站的油。
- 每当卡车将车内汽油用完时，选择已经经过的加油站中加油量最大的进行加油。看是否能到达下一个加油站。
- 则将已经经过的加油站的油量作为优先队列保存，每次取出最大值。

动物王国中有三类动物A,B,C，这三类动物的食物链构成了有趣的环形。A吃B， B吃C，C吃A。

现有N个动物，以1－N编号。每个动物都是A,B,C中的一种，但是我们并不知道它到底是哪一种。 有人用两种说法对这N个动物所构成的食物链关系进行描述： 第一种说法是"1 X Y"，表示X和Y是同类。 第二种说法是"2 X Y"，表示X吃Y。 此人对N个动物，用上述两种说法，一句接一句地说出K句话，这K句话有的是真的，有的是假的。当一句话满足下列三条之一时，这句话就是假话，否则就是真话。 1） 当前的话与前面的某些真的话冲突，就是假话； 2） 当前的话中X或Y比N大，就是假话； 3） 当前的话表示X吃X，就是假话。 你的任务是根据给定的N（1 <= N <= 50,000）和K句话（0 <= K <= 100,000），输出假话的总数。

第一行是两个整数N和K，以一个空格分隔。 以下K行每行是三个正整数 D，X，Y，两数之间用一个空格隔开，其中D表示说法的种类。 若D=1，则表示X和Y是同类。 若D=2，则表示X吃Y。

只有一个整数，表示假话的数目。

1 | 100 7 |

1 | 3 |

1 | `#include <cstdio>` |

https://blog.csdn.net/niushuai666/article/details/6981689

]]>Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).

Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.

Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.

Line 1: Three space-separated integers: N, ML, and MD.

Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.

Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

1 | 4 2 1 |

1 | 27 |

Explanation of the sample:

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

1 | `#include <queue>` |

- 线性规划问题 -> 单纯形法。
- 转化为最短路径问题（SPFA）。

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

* Line 1: The number of ponds in Farmer John's field.

1 | 10 12 |

1 | 3 |

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

1 | `#include <cstdio>` |

- 二维字符数组的输入问题，边界问题。
- 深度优先搜索遍历连通图，求连通子图的个数。
- 深度优先遍历的条件判定。

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediately falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.

1 | 2 |

1 | 4 8 |

1 | `#include <cstdio>` |

- 蚂蚁碰到后，各自掉头，其实因为速度不变，和没碰到没有任何区别，所以每只蚂蚁互不影响各自的状态，可以每只蚂蚁单独考虑。
- 在给定每只蚂蚁的坐标下，求最短时间，考虑一只蚂蚁，根据朝向有两种选择，哪个朝向到终点用时最短，则是该只蚂蚁的最短用时，如法炮制，求出所有蚂蚁各自的最短时长，最后取出最大值，是这群蚂蚁的最短用时。
- 最长时间同最短时间求法。