QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#332074#7737. Extending Distance275307894aTL 995ms4116kbC++143.3kb2024-02-19 07:54:492024-02-19 07:54:49

Judging History

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

  • [2024-02-19 07:54:49]
  • 评测
  • 测评结果:TL
  • 用时:995ms
  • 内存:4116kb
  • [2024-02-19 07:54:49]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=500+5,M=N*N+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
int n,m,k;
struct yyy{int to,w,g,z;};struct ljb{int head,h[N];yyy f[N*10];void add(int x,int y,int w,int g){f[++head]=(yyy){y,w,g,h[x]};h[x]=head;}}s;
void con(int x,int y,int w,int g){s.add(x,y,w,g);s.add(y,x,-w,0);}
int S,T;
namespace Dicnic{
	int d[N];
	int bfs(){
		queue<int> q;Me(d,0x3f);d[S]=0;q.emplace(S);
		while(!q.empty()){
			int x=q.front();q.pop();yyy tmp;//gdb(x,d[x]);
			for(int i=s.h[x];i;i=tmp.z){
				tmp=s.f[i];if(!tmp.g||d[tmp.to]<=d[x]+tmp.w) continue;
				d[tmp.to]=d[x]+tmp.w;q.emplace(tmp.to);
			}
		}
		return d[T]<=INF;
	}
	int vis[N];
	ll dfs(int x,ll sum){//gdb(x,sum);
		if(x==T) return sum;ll tot=0;yyy tmp;vis[x]=1;
		for(int i=s.h[x];i;i=tmp.z){
			tmp=s.f[i];if(!tmp.g||d[tmp.to]!=d[x]+tmp.w||vis[tmp.to]) continue;
			ll k=dfs(tmp.to,min(sum,1ll*tmp.g));s.f[i].g-=k;s.f[i^1].g+=k;sum-=k;tot+=k;if(!sum) break;
		}
		vis[x]=0;
		return tot;
	}
	ll calc(){
		ll ans=0;int ti=k;
		while(bfs()&&ti){
			if(!d[T]) dfs(S,1e18);
			else{
				int g=dfs(S,ti);
				ti-=g;ans+=g*d[T];
			}
		}
		return ans;
	}
}
int getid(int x,int y){
	if(!x) return 0;if(x==n) return T;
	return (x-1)*(m-1)+y;
}
int a1[N][N],a2[N][N];
void Solve(){
	int i,j;scanf("%d%d%d",&n,&m,&k);
	fill(s.h,s.h+n*m+3,0);s.head=1;
	S=0;T=(n-1)*(m-1)+1;
	for(i=1;i<=n;i++) for(j=1;j<m;j++){
		int x;scanf("%d",&x);a1[i][j]=x;
		int ix=getid(i-1,j),iy=getid(i,j);
		con(ix,iy,0,x);con(iy,ix,0,x);
		con(ix,iy,1,k);con(iy,ix,1,k);
	}
	for(i=1;i<n;i++) for(j=1;j<=m;j++){
		int x;scanf("%d",&x);a2[i][j]=x;
		if(j==1||j==m) continue;
		int ix=getid(i,j-1),iy=getid(i,j);
		con(ix,iy,0,x);con(iy,ix,0,x);
		con(ix,iy,1,k);con(iy,ix,1,k);
	}
	printf("%lld\n",Dicnic::calc());
	for(i=1;i<n;i++) for(j=1;j<m;j++){
		int id=getid(i,j);yyy tmp;
		for(int p=s.h[id];p;p=tmp.z){
			tmp=s.f[p];if(tmp.w<=0) continue;
			if(tmp.to==getid(i,j-1)&&j^1) a2[i][j]+=k-tmp.g;
			if(tmp.to==getid(i,j+1)&&j^m-1) a2[i][j+1]+=k-tmp.g;
			if(tmp.to==getid(i-1,j)) a1[i][j]+=k-tmp.g;
			if(tmp.to==getid(i+1,j)) a1[i+1][j]+=k-tmp.g;
		}
	}
	yyy tmp;for(int p=s.h[0];p;p=tmp.z){
		tmp=s.f[p];if(tmp.w<=0) continue;
		for(int i=1;i<m;i++) if(tmp.to==getid(1,i)) a1[1][i]+=k-tmp.g;
	}
	for(i=1;i<=n;i++) for(j=1;j<m;j++) printf("%d%c",a1[i][j]," \n"[j==m-1]);
	for(i=1;i<n;i++) for(j=1;j<=m;j++) printf("%d%c",a2[i][j]," \n"[j==m]);
}
int main(){
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4116kb

input:

2
3 4 6
2 1 15
7 1 9
13 3 2
3 6 1 2
5 2 15 3
3 3 3
1 1
2 2
3 3
1 1 1
2 2 2

output:

9
2 1 15
8 1 9
13 3 5
3 6 6 2
5 2 15 3
4
1 4
2 3
3 3
1 1 1
2 2 2

result:

ok Correct. (2 test cases)

Test #2:

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

input:

125
4 4 48
33 9 39
38 74 3
18 44 9
20 91 19
76 95 17 16
61 88 50 49
68 18 33 84
4 4 14
54 69 42
47 90 28
2 73 59
1 20 90
43 22 74 19
27 67 46 43
42 21 78 80
4 4 93
12 67 38
13 85 39
74 68 77
71 80 64
92 97 53 46
66 6 30 20
66 70 71 24
4 4 34
43 86 55
49 34 73
78 77 90
99 49 5
55 4 63 47
80 24 15 3
8...

output:

87
33 38 39
38 74 22
37 44 29
20 91 19
76 95 17 16
61 88 50 49
68 18 33 84
14
54 69 42
47 90 28
2 73 59
1 20 104
43 22 74 19
27 67 46 43
42 21 78 80
166
12 79 119
59 85 66
74 68 77
71 80 64
92 97 53 46
66 6 30 20
66 70 71 24
34
43 86 55
49 34 73
78 77 90
99 49 9
55 4 63 47
80 24 45 3
85 12 6 31
45
4...

result:

ok Correct. (125 test cases)

Test #3:

score: 0
Accepted
time: 71ms
memory: 3988kb

input:

80
5 5 97
10 18 14 13
17 15 16 11
15 10 14 15
12 17 12 15
12 11 15 15
19 19 13 19 19
19 17 10 10 19
12 13 18 11 20
12 17 14 13 12
5 5 65
13 15 13 20
18 19 13 20
10 19 18 17
19 19 11 14
12 18 11 10
18 14 18 19 18
20 11 17 11 17
16 13 19 18 13
16 14 17 11 18
5 5 3
15 10 10 18
17 17 17 14
13 15 15 19
1...

output:

473
10 18 14 108
26 15 20 89
35 16 20 79
48 17 17 68
57 20 18 55
19 19 13 19 19
19 17 10 10 19
12 13 18 11 20
12 17 14 13 12
271
13 15 13 75
27 19 14 56
34 19 18 45
42 24 11 39
36 18 16 46
18 14 18 19 18
20 11 17 11 17
16 13 19 18 13
16 14 17 11 18
3
15 10 10 21
17 17 17 14
13 15 15 19
10 18 16 17
1...

result:

ok Correct. (80 test cases)

Test #4:

score: 0
Accepted
time: 995ms
memory: 4008kb

input:

55
6 6 98
943830625 154853276 396311720 585129723 216006508
789713291 522861691 174874210 616414184 931597164
831871942 149821142 520941619 814161584 200419736
646044877 574761262 188910317 673355715 723256093
264106685 163628126 318085983 226850181 101764170
179381345 486537031 346100002 805792579 ...

output:

98
943830625 154853276 396311720 585129723 216006508
789713291 522861691 174874210 616414184 931597164
831871942 149821142 520941619 814161584 200419736
646044877 574761262 188910317 673355715 723256093
264106685 163628126 318085983 226850279 101764170
179381345 486537031 346100002 805792579 5081942...

result:

ok Correct. (55 test cases)

Test #5:

score: 0
Accepted
time: 290ms
memory: 3880kb

input:

55
6 6 96
16739843 17336211 10384494 17185421 10646458
18552039 13790956 13642043 10307995 14193711
19297204 12810329 18681558 18724838 16636750
11505737 19658923 19307194 12241535 15070027
16123862 17524159 19471242 14316479 10954501
10604286 17691735 17352365 14092770 19909253
11761060 19372581 16...

output:

96
16739843 17336211 10384494 17185421 10646458
18552039 13790956 13642043 10307995 14193807
19297204 12810329 18681558 18724838 16636750
11505737 19658923 19307194 12241535 15070027
16123862 17524159 19471242 14316479 10954501
10604286 17691735 17352365 14092770 19909253
11761060 19372581 16863139 ...

result:

ok Correct. (55 test cases)

Test #6:

score: -100
Time Limit Exceeded

input:

40
7 7 17
27500 8466 7754 5254 45204 36821
35457 23725 45494 26962 24728 31437
46232 38720 23756 17265 21004 25959
29727 6060 23244 45236 39610 23968
17068 14954 9745 28949 20957 30885
8272 49710 28660 17038 12058 48058
10306 5065 45011 25264 25765 17423
21072 22743 17503 11324 10214 6879 22253
1729...

output:


result: