QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#507874#4128. 费用流hhy0613100 ✓4ms3688kbC++141.9kb2024-08-06 22:18:302024-08-06 22:18:31

Judging History

你现在查看的是最新测评结果

  • [2024-08-06 22:18:31]
  • 评测
  • 测评结果:100
  • 用时:4ms
  • 内存:3688kb
  • [2024-08-06 22:18:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=105,M=1005,INF=0x3f3f3f3f;
namespace Flow{
	int s,t,head[N],cnt,cur[N],d[N],qe[N];
	struct edge{
		int v,cap,flow,next;
	}e[M<<1];
	void add_edge(int u,int v,int cap,int flow){
		e[cnt]=edge{v,cap,flow,head[u]};
		head[u]=cnt++;
	}
	void Add_Edge(int u,int v,int cap){
		add_edge(u,v,cap,0);
		add_edge(v,u,0,0);
	}
	void init(int ss,int tt){
		s=ss;
		t=tt;
		memset(head,255,sizeof(head));
		cnt=0;
	}
	bool BFS(){
		memcpy(cur,head,sizeof(head));
		memset(d,255,sizeof(d));
		int l=0,r=0;
		qe[r++]=s;
		d[s]=0;
		while(l<r){
			int u=qe[l++];
			for(int i=head[u];~i;i=e[i].next){
				const int v=e[i].v;
				if(e[i].cap>e[i].flow && d[v]==-1){
					d[v]=d[u]+1;
					qe[r++]=v;
					if(v==t) return true;
				}
			}
		}
		return false;
	}
	int DFS(int u,int now){
		if(u==t) return now;
		int flow=0;
		for(int i=cur[u];~i && now>0;i=cur[u]){
			const int v=e[i].v;
			cur[u]=e[i].next;
			if(e[i].cap>e[i].flow && d[v]==d[u]+1){
				const int t=DFS(v,min(now,e[i].cap-e[i].flow));
				flow+=t;
				now-=t;
				e[i].flow+=t;
				e[i^1].flow-=t;
				if(t==0) d[v]=-1;
			}
		}
		return flow;
	}
	int Dinic(){
		int flow=0;
		while(BFS()) flow+=DFS(s,INF);
		return flow;
	}
}
struct edge{
	int u,v,w;
}e[M];
int ff,n,m,P;
bool check(int mid){
	Flow::init(0,n-1);
	for(int i=0;i<m;++i) Flow::Add_Edge(e[i].u,e[i].v,min(e[i].w,mid));
	return (Flow::Dinic()==ff); 
}
int main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	cin >> n >> m >> P;
	for(int i=0;i<m;++i){
		cin >> e[i].u >> e[i].v >> e[i].w;
		--e[i].u;
		--e[i].v;
	}
	Flow::init(0,n-1);
	for(int i=0;i<m;++i) Flow::Add_Edge(e[i].u,e[i].v,e[i].w);
	cout << (ff=Flow::Dinic()) << '\n';
	int l=0,r=1000000005,ans=-1;
	while(l<r){
		int mid=l+(r-l)/2;
		if(check(mid)){
			ans=mid;
			r=mid;
		}else l=mid+1;
	}
	cout << ans*P << endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 3648kb

input:

70 1000 1
25 26 821
26 27 467
27 28 689
28 29 131
29 30 998
30 31 299
31 32 237
32 33 692
33 34 531
34 35 938
35 36 338
36 37 986
37 38 182
38 39 533
39 40 395
40 41 117
41 42 901
42 43 232
43 44 711
44 45 539
45 46 760
46 47 497
47 48 27
48 49 916
49 50 535
50 51 771
51 52 30
52 53 405
53 54 257
54...

output:

19749
9875

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000050635)

Test #2:

score: 10
Accepted
time: 2ms
memory: 3596kb

input:

100 1000 10
25 26 390
26 27 715
27 28 480
28 29 397
29 30 24
30 31 95
31 32 233
32 33 560
33 34 404
34 35 266
35 36 94
36 37 946
37 38 510
38 39 873
39 40 472
40 41 90
41 42 573
42 43 501
43 44 414
44 45 387
45 46 391
46 47 915
47 48 313
48 49 77
49 50 522
50 51 773
51 52 816
52 53 746
53 54 996
54 ...

output:

7105
35530

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000140746)

Test #3:

score: 10
Accepted
time: 3ms
memory: 3624kb

input:

100 1000 10
25 26 338
26 27 692
27 28 457
28 29 540
29 30 725
30 31 715
31 32 758
32 33 534
33 34 347
34 35 302
35 36 125
36 37 4
37 38 171
38 39 910
39 40 55
40 41 954
41 42 438
42 43 186
43 44 725
44 45 512
45 46 898
46 47 436
47 48 273
48 49 469
49 50 376
50 51 509
51 52 245
52 53 104
53 54 70
54...

output:

7869
39350

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000127081)

Test #4:

score: 10
Accepted
time: 3ms
memory: 3664kb

input:

60 1000 1
25 26 128
26 27 415
27 28 198
28 29 649
29 30 101
30 31 195
31 32 573
32 33 598
33 34 758
34 35 35
35 36 146
36 37 457
37 38 936
38 39 479
39 40 378
40 41 871
41 42 482
42 43 627
43 44 272
44 45 484
45 46 159
46 47 217
47 48 353
48 49 109
49 50 576
50 51 388
51 52 847
52 53 711
53 54 312
5...

output:

20435
10218

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000048936)

Test #5:

score: 10
Accepted
time: 2ms
memory: 3612kb

input:

75 1000 5
25 26 1
26 27 53
27 28 300
28 29 595
29 30 413
30 31 955
31 32 754
32 33 594
33 34 641
34 35 372
35 36 548
36 37 935
37 38 437
38 39 694
39 40 804
40 41 912
41 42 576
42 43 611
43 44 115
44 45 114
45 46 912
46 47 711
47 48 841
48 49 789
49 50 564
50 51 269
51 52 278
52 53 819
53 54 987
54 ...

output:

11780
29450

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000000000)

Test #6:

score: 10
Accepted
time: 4ms
memory: 3652kb

input:

55 600 9
21 22 844
22 23 799
23 24 352
24 25 793
25 26 625
26 27 815
27 28 43
28 29 628
29 30 701
30 31 70
31 32 537
32 33 515
33 34 541
34 35 283
35 36 137
36 37 734
37 38 522
38 39 312
39 40 583
40 41 785
41 42 434
42 43 506
43 44 313
44 45 501
45 46 374
46 47 356
47 48 507
48 49 69
49 50 386
50 5...

output:

15077
67851

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000066326)

Test #7:

score: 10
Accepted
time: 1ms
memory: 3684kb

input:

50 1000 2
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 1
7 8 1
8 9 1
9 10 1
10 11 1
11 12 1
12 13 1
13 14 1
14 15 1
15 16 1
16 17 1
17 18 1
18 19 1
19 20 1
20 21 1
21 22 1
22 23 1
23 24 1
24 25 1
25 26 1
26 27 1
27 28 1
28 29 1
29 30 1
30 31 1
31 32 1
32 33 1
33 34 1
34 35 1
35 36 1
36 37 1
37 38 1
38 39 1
39 ...

output:

29
2

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000000000)

Test #8:

score: 10
Accepted
time: 1ms
memory: 3684kb

input:

50 1000 3
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 1
7 8 1
8 9 1
9 10 1
10 11 1
11 12 1
12 13 1
13 14 1
14 15 1
15 16 1
16 17 1
17 18 1
18 19 1
19 20 1
20 21 1
21 22 1
22 23 1
23 24 1
24 25 1
25 26 1
26 27 1
27 28 1
28 29 1
29 30 1
30 31 1
31 32 1
32 33 1
33 34 1
34 35 1
35 36 1
36 37 1
37 38 1
38 39 1
39 ...

output:

34
3

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000000000)

Test #9:

score: 10
Accepted
time: 1ms
memory: 3572kb

input:

50 300 8
17 18 11
18 19 385
19 20 167
20 21 155
21 22 550
22 23 789
23 24 197
24 25 689
25 26 480
26 27 585
27 28 406
28 29 79
29 30 189
30 31 578
31 32 728
32 33 660
33 34 350
34 35 987
35 36 517
36 37 612
37 38 870
38 39 846
39 40 115
40 41 318
41 42 318
42 43 911
43 44 812
44 45 269
45 46 66
46 4...

output:

4965
19864

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000201410)

Test #10:

score: 10
Accepted
time: 4ms
memory: 3688kb

input:

80 1000 6
25 26 286
26 27 77
27 28 378
28 29 452
29 30 481
30 31 335
31 32 52
32 33 564
33 34 290
34 35 105
35 36 693
36 37 469
37 38 8
38 39 658
39 40 45
40 41 817
41 42 711
42 43 694
43 44 804
44 45 813
45 46 637
46 47 190
47 48 825
48 49 397
49 50 766
50 51 301
51 52 441
52 53 461
53 54 145
54 55...

output:

10502
31506

result:

points 1.0 First Answer Correct, Second Answer Correct (error = 0.000000000)