Arrest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1731 Accepted Submission(s): 682
Problem Description
There are (N+1) cities on TAT island. City 0 is where police headquarter located. The economy of other cities numbered from 1 to N ruined these years because they are all controlled by mafia. The police plan to catch all the mafia gangs in these N cities all over the year, and they want to succeed in a single mission. They figure out that every city except city 0 lives a mafia gang, and these gangs have a simple urgent message network: if the gang in city i (i>1) is captured, it will send an urgent message to the gang in city i-1 and the gang in city i -1 will get the message immediately.
The mission must be carried out very carefully. Once a gang received an urgent message, the mission will be claimed failed.
You are given the map of TAT island which is an undirected graph. The node on the graph represents a city, and the weighted edge represents a road between two cities(the weight means the length). Police headquarter has sent k squads to arrest all the mafia gangs in the rest N cities. When a squad passes a city, it can choose to arrest the gang in the city or to do nothing. These squads should return to city 0 after the arrest mission.
You should ensure the mission to be successful, and then minimize the total length of these squads traveled.
Input
There are multiple test cases.
Each test case begins with a line with three integers N, M and k, here M is the number of roads among N+1 cities. Then, there are M lines. Each of these lines contains three integers X, Y, Len, which represents a Len kilometer road between city X and city Y. Those cities including city 0 are connected.
The input is ended by “0 0 0”.
Restrictions: 1 ≤ N ≤ 100, 1 ≤ M ≤ 4000, 1 ≤ k ≤ 25, 0 ≤ Len ≤ 1000
Output
For each test case,output a single line with a single integer that represents the minimum total length of these squads traveled.
Sample Input
3 4 2 0 1 3 0 2 4 1 3 2 2 3 2 0 0 0
Sample Output
题意:有n+1个城市(0 - n)和m条无向边。已知在1 - n城市均有一个盗贼,在城市0有k个警察。现在去抓这n个盗贼,要求抓第i个盗贼前必须先抓到第i-1个,且抓完n个盗贼后要返回城市0。问抓到所有盗贼警察们需要走的最小路程和。
思路:明显的费用流。预处理下最短路。建边时拆点就可以了,因为需要的是最小费用,每次找增广路找的是最小花费路,那么把边的费用设为-INF就可以了。
建边:源点到0引容量为k、费用0的边。1-n拆点i - i+n。i+n -> j建边(i < j)。因为需要返回i+n -> 汇点建边。
注意:k个警察一起去抓不一定是最优解,这点0到汇点建容量为k、费用为0的边就可以了。
AC代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include