分类 数据结构与算法 下的文章

POJ 2135 Farm Tour(最小费用最大流)解题报告


解题思路

使用费用流求解。首先把题中所有的边都按流量为 1,费用为路径长度加边,这样就保证了每条路只能走一次,且求出来的最小费用就是最短路径长度。之后再把源点到 1 以及 n 到汇点都加一条流量为 2,费用为 0 的边,这是为了支持题目中的往返操作。这样跑一边最小费用最大流即可求出答案。


POJ 4047 Garden(线段树 - 区间增减、区间查询)解题报告


题面

Garden
Time Limit: 1000MS Memory Limit: 65536K

Description
There are n flowerpots in Daniel's garden. These flowerpots are placed in n positions, and these n positions are numbered from 1 to n. Each flower is assigned an aesthetic value. The aesthetic values vary during different time of a day and different seasons of a year. Naughty Daniel is a happy and hardworking gardener who takes pleasure in exchanging the position of the flowerpots.
Friends of Daniel are great fans of his miniature gardens. Before they visit Daniel's home, they will take their old-fashioned cameras which are unable to adjust the focus so that it can give a shot of exactly k consecutive flowerpots. Daniel hopes his friends enjoy themselves, but he doesn't want his friend to see all of his flowers due to some secret reasons, so he guides his friends to the best place to catch the most beautiful view in the interval [x, y], that is to say, to maximize the sum of the aesthetics values of the k flowerpots taken in one camera shot when they are only allow to see the flowerpots between position x to position y.
There are m operations or queries are given in form of (p, x, y), here p = 0, 1 or 2. The meanings of different value of p are shown below.

  1. p=0 settheaestheticvalueofthepotinpositionxasy.(1<=x<=n;-100<= y <= 100)
  2. p = 1 exchange the pot in position x and the pot in position y. (1 <= x, y <= n; x might equal to y)
  3. p = 2 print the maximum sum of aesthetics values of one camera shot in interval [x, y]. (1 <= x <= y <= n; We guarantee that y-x+1>=k)

Input
There are multiple test cases.
The first line of the input file contains only one integer indicates the number of test cases.
For each test case, the first line contains three integers: n, m, k (1 <= k <= n <= 200,000; 0 <= m <= 200,000).
The second line contains n integers indicates the initial aesthetic values of flowers from position 1 to position n. Some flowers are sick, so their aesthetic values are negative integers. The aesthetic values range from -100 to 100. (Notice: The number of position is assigned 1 to n from left to right.)
In the next m lines, each line contains a triple (p, x, y). The meaning of triples is mentioned above.

Output
For each query with p = 2, print the maximum sum of the aesthetics values in oneshot in interval [x, y].

Sample Input
1
5 7 3
-1 2 -4 6 1
2 1 5
2 1 3
1 2 1
2 1 5
2 1 4
0 2 4
2 1 5

Sample Output
4
-3
3
1
6



























Codeforces 429B Working out(动态规划)解题报告


题目链接

题目大意

两个人在一个有 n×m 个房间的健身房内健身,每个房间都有一个锻炼可消耗的卡路里数。一个人从 a1 一路锻炼到 an ,每次只能从 ai 移动到 ai+1 或 ai;另一个从 an 一路锻炼到 a1,每次只能从 ai 移动到 ai 或 ai-1 。每在一个房间锻炼可以消耗对应的卡路里数,两个人必须在中间的某一个房间碰面且只能碰面一次,这个房间不计算卡路里数。求两人如何走,能使两人总计消耗卡路里数最大。