Random Maze(福州网络赛)

来源:互联网 发布:java关键词过滤 编辑:程序博客网 时间:2024/05/18 09:16

                                                 Random Maze

                                                 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                      Total Submission(s): 906    Accepted Submission(s): 297

Problem Description
In the game “A Chinese Ghost Story”, there are many random mazes which have some characteristic:
1.There is only one entrance and one exit.
2.All the road in the maze are unidirectional.
3.For the entrance, its out-degree = its in-degree + 1.
4.For the exit, its in-degree = its out-degree + 1.
5.For other node except entrance and exit, its out-degree = its in-degree.

There is an directed graph, your task is removing some edge so that it becomes a random maze. For every edge in the graph, there are two values a and b, if you remove the edge, you should cost b, otherwise cost a.
Now, give you the information of the graph, your task if tell me the minimum cost should pay to make it becomes a random maze.

Input
The first line of the input file is a single integer T.
The rest of the test file contains T blocks.
For each test case, there is a line with four integers, n, m, s and t, means that there are n nodes and m edges, s is the entrance's index, and t is the exit's index. Then m lines follow, each line consists of four integers, u, v, a and b, means that there is an edge from u to v.
2<=n<=100, 1<=m<=2000, 1<=s, t<=n, s != t. 1<=u, v<=n. 1<=a, b<=100000
 
Output
For each case, if it is impossible to work out the random maze, just output the word “impossible”, otherwise output the minimum cost.(as shown in the sample output)
 
Sample Input
22 1 1 22 1 2 35 6 1 41 2 3 12 5 4 55 3 2 33 2 6 72 4 7 63 4 10 5
 

Sample Output
Case 1: impossibleCase 2: 27
 

Source
The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest
 
原创粉丝点击