CSU 1506 Double Shortest Paths

1506: Double Shortest Paths

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 49  Solved: 5

Description

CSU 1506 Double Shortest Paths_第1张图片

 

Input

There will be at most 200 test cases. Each case begins with two integers n, m (1<=n<=500, 1<=m<=2000), the number of caves and passages. Each of the following m lines contains four integers u, v, di and ai (1<=u,v<=n, 1<=di<=1000, 0<=ai<=1000). Note that there can be multiple passages connecting the same pair of caves, and even passages connecting a cave and itself.

 

Output

For each test case, print the case number and the minimal total difficulty.

 

Sample Input

4 4
1 2 5 1
2 4 6 0
1 3 4 0
3 4 9 1
4 4
1 2 5 10
2 4 6 10
1 3 4 10
3 4 9 10

Sample Output

Case 1: 23
Case 2: 24

HINT

 

Source

湖南省第十届大学生计算机程序设计竞赛

 

解题:费用流

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 1000;
18 struct arc{
19     int to,flow,cost,next;
20     arc(int x = 0,int y = 0,int z = 0,int nxt = -1){
21         to = x;
22         flow = y;
23         cost = z;
24         next = nxt;
25     }
26 };
27 arc e[maxn*maxn];
28 int head[maxn],d[maxn],p[maxn];
29 int tot,S,T;
30 void add(int u,int v,int flow,int cost){
31     e[tot] = arc(v,flow,cost,head[u]);
32     head[u] = tot++;
33     e[tot] = arc(u,0,-cost,head[v]);
34     head[v] = tot++;
35 }
36 bool in[maxn];
37 bool spfa(){
38     queue<int>q;
39     for(int i = S; i <= T; ++i){
40         p[i] = -1;
41         in[i] = false;
42         d[i] = INF;
43     }
44     d[S] = 0;
45     q.push(S);
46     while(!q.empty()){
47         int u = q.front();
48         q.pop();
49         in[u] = false;
50         for(int i = head[u]; ~i; i = e[i].next){
51             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
52                 d[e[i].to] = d[u] + e[i].cost;
53                 p[e[i].to] = i;
54                 if(!in[e[i].to]){
55                     in[e[i].to] = true;
56                     q.push(e[i].to);
57                 }
58             }
59         }
60     }
61     return p[T] > -1;
62 }
63 int solve(){
64     int ans = 0;
65     while(spfa()){
66         int minF = INF;
67         for(int i = p[T]; ~i; i = p[e[i^1].to])
68             minF = min(minF,e[i].flow);
69         for(int i = p[T]; ~i; i = p[e[i^1].to]){
70             e[i].flow -= minF;
71             e[i^1].flow += minF;
72         }
73         ans += d[T]*minF;
74     }
75     return ans;
76 }
77 int main(){
78     int n,m,u,v,ai,di,cs = 1;
79     while(~scanf("%d %d",&n,&m)){
80         memset(head,-1,sizeof(head));
81         S = tot = 0;
82         T = n + 1;
83         for(int i = 0; i < m; ++i){
84             scanf("%d %d %d %d",&u,&v,&ai,&di);
85             add(u,v,1,ai);
86             add(u,v,1,ai+di);
87         }
88         add(S,1,2,0);
89         add(n,T,2,0);
90         printf("Case %d: %d\n",cs++,solve());
91     }
92     return 0;
93 }
View Code

 

你可能感兴趣的:(double)