UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#198720#2701. 排列ysys10015ms1412kbC++112.4kb2023-11-30 09:04:322023-11-30 12:11:37

answer

#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(b);i>=(a);i--)
#define endl "\n"
#define For(i,u) for(int i=head[u];i;i=e[i].next)
#define V vector<int>
#define VV vector<V>
#define Debug(a) cout<<"QwQ "<<a<<endl;
const int MOD=1e9+7;

using namespace std;

int n,m,s,t;

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

string reads()
{
	char ch=getchar();string s;
	while(ch<'a'||ch>'z'){ch=getchar();}
	while(ch>='a'&&ch<='z'){s+=ch;ch=getchar();}
	return s;
}

char readc()
{
	char ch=getchar();
	while(ch<'a'||ch>'z') ch=getchar();
	return ch;
}

// down is mytrue code ------------------------------

const int M=5050,N=1010,INF=(1ll<<31);

struct
{
	int to,dis,next;
}e[M*4];

int cnt=1,head[N],ans,dis[N],cur[N];

void add(int u,int v,int w)
{
	e[++cnt].to=v;
	e[cnt].next=head[u];
	e[cnt].dis=w;
	head[u]=cnt;
	swap(u,v);
	w=0;
	e[++cnt].to=v;
	e[cnt].next=head[u];
	e[cnt].dis=w;
	head[u]=cnt;
}

bool bfs()
{
	memset(dis,-1 ,sizeof(dis));
	dis[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=0;i=e[i].next)
		{
			int v=e[i].to;
		//if(dis[v]&&e[i].dis==0)
			if((dis[v]==-1)&&(e[i].dis!=0))
			{
				dis[v]=dis[u]+1;
				q.push(v);
			}
		}
	}
	if(dis[t]==-1)
	{
		return false;
	}
	else return true;
}
int dfs(int u,int flo)
{
	if(u==t) {
		ans+=flo;
		return flo;
	}
	int used=0;
	for(int i=cur[u];i!=0;i=e[i].next)
	{
		cur[u]=e[i].next;
		int v=e[i].to;
		if(dis[v]==dis[u]+1&&e[i].dis>0)
		{
			int d=dfs(v,min(flo-used,e[i].dis));
			if(d){
				e[i].dis-=d;e[i^1].dis+=d;used+=d;
			}
			if(used==flo)return flo;
		}
	}
	return used;
}
void run()
{
	while(bfs())
	{
		for(int i=1;i<=n*2+2;i++)cur[i]=head[i]; 
		dfs(s,INF);
	}
}

int a[N];

signed main()
{
//	freopen("dt.in","r",stdin);
	n=read(),m=read();
	s=n*2+2,t=n*2+1;
	int sum=0;
	rep(i,1,n)
	{
		a[i]=read();
		if(a[i]>=0) add(s,i,a[i]),add(i,i+n,0),add(i+n,t,a[i]),sum+=a[i];
		else add(s,i,0),add(i,i+n,-a[i]),add(i+n,t,0);
	} 
	rep(i,1,m)
	{
		int u=read(),v=read();
		add(u,v,INF);
		add(u+n,v+n,INF);
	}
	run();
	cout<<sum-ans<<endl;
	return 0;
}


详细

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

Subtask #1:

score: 5
Accepted

Test #1:

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

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: 1ms
memory: 1220kb

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: 0ms
memory: 1228kb

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: 1224kb

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: 0ms
memory: 1224kb

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: 1228kb

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: 1224kb

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: 1224kb

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: 1360kb

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: 0ms
memory: 1364kb

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: 2ms
memory: 1364kb

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: 1324kb

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: 4ms
memory: 1376kb

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: 3ms
memory: 1412kb

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: 2ms
memory: 1412kb

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: 0ms
memory: 1412kb

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: 3ms
memory: 1412kb

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: 0ms
memory: 1412kb

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