UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198734#2701. 排列snow_trace10022ms6160kbC++113.0kb2023-11-30 10:54:222023-11-30 12:13:39

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 2000000005;
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
vector<int>p[200005];
struct edge{
	int fl,to;
}e[200005];
int n,m,s,t,head = 1;
int cur[2005],dis[2005];
void add(int a,int b,int c){
	e[++head].to = b,e[head].fl = c;
	p[a].push_back(head);
	e[++head].to = a,e[head].fl = 0;
	p[b].push_back(head);return;
}
bool bfs(){
	memset(cur,0,sizeof(cur));
	memset(dis,63,sizeof(dis));
	queue<int>q;
	q.push(s);dis[s] = 0;
	//cout << s << endl;
	while(!q.empty()){
		int now = q.front();q.pop();
		//cout << now << endl;
		for(int i =0;i<p[now].size();i++){
			int to = e[p[now][i]].to,fl = e[p[now][i]].fl;
		//	cout << " " << to << endl;
			if(!fl)continue;
			if(dis[now]+1<dis[to]){
				dis[to] = dis[now]+1;
				q.push(to);
			}
		}
	}return (dis[t]<=2*n);
}int dfs(int now,int f){
	if(now == t or !f)return f;
	int res = 0;
	for(int i = cur[now];i<p[now].size();i++){
		cur[now] = i;
		int to = e[p[now][i]].to,fl = e[p[now][i]].fl,nw = 0;
		if(!fl or dis[now]+1!=dis[to])continue;
		if(nw = dfs(to,min(f,fl))){
			res+=nw,f-=nw;
			e[p[now][i]].fl-=nw,e[p[now][i]^1].fl+=nw;
			if(!f)break;
		}
	}return res;
}int Dinic(){
	int ans = 0;
	while(bfs()){
	//	cout << 1 << endl;
		ans+=dfs(s,inf);
	}return ans;
}int x[200005]; 
signed main(){
	cin>>n>>m;s = 2*n+1,t = 2*n+2;
	for(int i = 1;i<=n;i++)cin >> x[i];
	for(int i = 1;i<=m;i++){
		int a,b;cin >> a >> b;
		add(a,b,inf);add(a+n,b+n,inf);
	}for(int i = 1;i<=n;i++){
		if(x[i]>=0)add(s,i,x[i]),add(i+n,t,x[i]);
		else add(i,i+n,-x[i]);
	}int sum = 0;for(int i = 1;i<=n;i++)sum+=x[i]*(x[i]>=0);
	int res = Dinic();
	cout << sum-res << endl;
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 0ms
memory: 5984kb

input:

10 10
52 -599 872 588 665 116 78 -104 961 272
3 9
7 6
2 5
6 5
7 9
8 5
7 5
6 5
1 6
8 9

output:

3604

result:

ok single line: '3604'

Test #2:

score: 0
Accepted
time: 3ms
memory: 5988kb

input:

10 20
-968 377 -197 283 -414 101 -295 -575 693 669
2 6
3 8
2 6
3 8
5 3
1 8
10 3
6 8
1 8
10 9
5 9
10 ...

output:

1926

result:

ok single line: '1926'

Subtask #2:

score: 20
Accepted

Test #3:

score: 20
Accepted
time: 4ms
memory: 5984kb

input:

20 20
-728 107 -960 -614 -923 -170 269 -934 -425 -133 714 394 -233 378 409 -478 903 -324 757 -154
15...

output:

3553

result:

ok single line: '3553'

Test #4:

score: 0
Accepted
time: 0ms
memory: 5984kb

input:

20 20
889 352 -142 839 455 636 658 -368 -418 -690 -329 376 821 467 420 -382 -46 -257 -96 49
6 1
12 1...

output:

5962

result:

ok single line: '5962'

Test #5:

score: 0
Accepted
time: 3ms
memory: 5988kb

input:

20 30
-887 -515 -105 -458 254 228 301 -38 288 148 -97 394 -307 -472 -348 -113 245 622 572 -843
12 3
...

output:

3052

result:

ok single line: '3052'

Test #6:

score: 0
Accepted
time: 0ms
memory: 5984kb

input:

20 15
-653 -195 944 228 581 605 409 -34 268 -178 -918 -988 923 -76 469 912 151 522 -90 -600
8 2
6 8
...

output:

5978

result:

ok single line: '5978'

Test #7:

score: 0
Accepted
time: 0ms
memory: 5984kb

input:

20 50
-783 799 453 -988 -90 -222 -246 -395 -227 -965 693 -253 -336 -523 -862 -492 931 -641 768 226
1...

output:

3422

result:

ok single line: '3422'

Test #8:

score: 0
Accepted
time: 0ms
memory: 5984kb

input:

20 10
199 -226 137 708 -417 -238 630 383 727 -98 -71 -310 -285 830 -864 530 781 -486 133 -864
13 19
...

output:

5058

result:

ok single line: '5058'

Subtask #3:

score: 19
Accepted

Test #9:

score: 19
Accepted
time: 0ms
memory: 6088kb

input:

500 499
-50 -537 -338 -260 -168 -989 -62 221 243 -320 0 -415 -639 -938 407 -697 6 -685 -463 -541 44 ...

output:

38900

result:

ok single line: '38900'

Test #10:

score: 0
Accepted
time: 4ms
memory: 6096kb

input:

500 499
735 -860 902 192 45 -84 -855 918 -24 -604 -513 960 552 -917 -936 87 -854 -497 874 856 469 46...

output:

110572

result:

ok single line: '110572'

Test #11:

score: 0
Accepted
time: 0ms
memory: 6096kb

input:

500 499
-380 565 584 572 -953 -537 -866 -669 -498 210 697 324 -715 967 447 -59 517 416 367 216 885 5...

output:

122137

result:

ok single line: '122137'

Subtask #4:

score: 56
Accepted

Test #12:

score: 56
Accepted
time: 0ms
memory: 6108kb

input:

50 1000
898 -651 -135 85 374 329 -831 -188 776 650 737 569 72 404 -733 -658 -775 -957 904 128 676 38...

output:

6868

result:

ok single line: '6868'

Test #13:

score: 0
Accepted
time: 0ms
memory: 6128kb

input:

300 1000
-977 515 239 807 -830 763 121 947 244 158 -347 -91 178 250 584 744 860 931 100 -527 101 -57...

output:

43721

result:

ok single line: '43721'

Test #14:

score: 0
Accepted
time: 4ms
memory: 6152kb

input:

500 1000
263 832 758 -424 671 -22 408 24 971 99 263 -223 317 888 -720 -483 493 380 652 -440 -844 -17...

output:

98622

result:

ok single line: '98622'

Test #15:

score: 0
Accepted
time: 0ms
memory: 6148kb

input:

500 1000
-752 -351 -194 -490 998 355 -236 -724 950 996 -585 -74 -453 62 568 -989 399 306 -11 -196 69...

output:

91957

result:

ok single line: '91957'

Test #16:

score: 0
Accepted
time: 2ms
memory: 6160kb

input:

500 1000
234 -810 856 196 -704 -20 624 -720 930 671 -157 546 -472 -294 606 36 -944 -265 106 47 -274 ...

output:

103473

result:

ok single line: '103473'

Test #17:

score: 0
Accepted
time: 0ms
memory: 6152kb

input:

500 1000
504 -793 634 -724 801 186 115 -206 -394 38 163 -253 -563 -953 -881 364 -493 -685 -776 -975 ...

output:

94898

result:

ok single line: '94898'

Test #18:

score: 0
Accepted
time: 2ms
memory: 6144kb

input:

500 1000
-508 -217 -788 -837 46 -750 -422 -155 -765 -294 -505 -249 -625 493 -944 -712 235 252 162 10...

output:

33156

result:

ok single line: '33156'

Extra Test:

score: 0
Extra Test Passed