QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#332071#7737. Extending Distance275307894aWA 0ms4124kbC++143.2kb2024-02-19 07:47:082024-02-19 07:47:09

Judging History

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

  • [2024-02-19 07:47:09]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4124kb
  • [2024-02-19 07:47:08]
  • 提交

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;
		}
	}
	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: 0
Wrong Answer
time: 0ms
memory: 4124kb

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 1
2 3
3 3
1 1 1
2 2 2

result:

wrong answer The output T doesn't match number of operations. (test case 2)