# BZOJ1658 [Usaco2006 Mar]Water Slides 滑水

（1）把点分成出度>入度和入度>出度两种

（2）跑一遍网络流

 1 /**************************************************************
2     Problem: 1658
3     User: rausen
4     Language: C++
5     Result: Accepted
6     Time:32 ms
7     Memory:1040 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <algorithm>
12
13 using namespace std;
14
15 int n, m, ans, cnt1, cnt2, X, Y;
16 int deg[15000], start[15000], stop[15000], pos[15000];
17
18 int main(){
19     scanf("%d%d", &n, &m);
20     for (int i = 1; i <= n; ++i)
21         scanf("%d", pos + i);
22     for (int i = 1; i <= m; ++i){
23         scanf("%d%d", &X, &Y);
24         ++deg[X], --deg[Y];
25     }
26
27     for (int i = 1; i <= n; ++i){
28         for(; deg[i] > 0; --deg[i]) start[++cnt1] = pos[i];
29         for(; deg[i] < 0; ++deg[i]) stop[++cnt2] = pos[i];
30     }
31
32     sort(start + 1, start + cnt1 + 1);
33     sort(stop + 1, stop + cnt2 + 1);
34
35     for (int i = 1; i <= cnt1; ++i)
36         ans += abs(start[i] - stop[i]);
37     printf("%d\n", ans);
38     return 0;
39 }
View Code

posted on 2014-10-17 22:48  Xs酱~  阅读(435)  评论(0编辑  收藏  举报