QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#345525#7737. Extending DistanceSorting#WA 1ms5912kbC++203.4kb2024-03-07 05:09:472024-03-07 05:09:48

Judging History

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

  • [2024-03-07 05:09:48]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5912kb
  • [2024-03-07 05:09:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
int n,m;
ll k;
ll ans=0;
ll r[505][505];
ll c[505][505];
int ec=0;
int f[2005];ll w[2005];
int eu[2005],ev[2005];
vector<pair<int,int> >adj[505];


ll pot[505];
void add(int u,int v,ll c){
	adj[u].push_back({ec,v});
	adj[v].push_back({ec+1,u});
	w[ec/2]=c;f[ec/2]=0;
	eu[ec/2]=u;
	ev[ec/2]=v;
	ec+=2;
}
ll dist[505];
bool vis[505];
int prv[505];
void iu(){
	for(int i=1; i<=n*m ;i++){
		dist[i]=1e18;
		vis[i]=false;
		prv[i]=0;
	}
	typedef pair<ll,int> pli;
	priority_queue<pli,vector<pli>,greater<pli> >pq;
	for(int i=1; i<=n ;i++){
		dist[(i-1)*m+1]=0;
		pq.push({0,(i-1)*m+1});
	}
	while(!pq.empty()){
		int x=pq.top().se;pq.pop();
		if(vis[x]) continue;
		for(auto c:adj[x]){
			int ec=c.fi/2;
			if(c.fi%2){
				ll cw=1e18;
				if(f[ec]==0) cw=w[ec];
				if(f[ec]==1) cw=-w[ec];
				if(dist[c.se]>dist[x]+cw){
					dist[c.se]=dist[x]+cw;
					prv[c.se]=c.fi;
					pq.push({dist[c.se]-pot[c.se],c.se});
				}
			}
			else{
				ll cw=1e18;
				if(f[ec]==0) cw=w[ec];
				if(f[ec]==-1) cw=-w[ec];
				if(dist[c.se]>dist[x]+cw){
					dist[c.se]=dist[x]+cw;
					prv[c.se]=c.fi;
					pq.push({dist[c.se]-pot[c.se],c.se});
				}
			}
		}
	}
	int god=m;
	for(int i=1; i<=n ;i++){
		if(dist[i*m]<dist[god]) god=i*m;
	}
	k=dist[god]+k;
}
bool jieun(){
	for(int i=1; i<=n*m ;i++){
		dist[i]=k;
		vis[i]=false;
		prv[i]=0;
	}
	typedef pair<ll,int> pli;
	priority_queue<pli,vector<pli>,greater<pli> >pq;
	for(int i=1; i<=n ;i++){
		dist[(i-1)*m+1]=0;
		pq.push({0,(i-1)*m+1});
	}
	while(!pq.empty()){
		int x=pq.top().se;pq.pop();
		if(vis[x]) continue;
		for(auto c:adj[x]){
			int ec=c.fi/2;
			if(c.fi%2){
				ll cw=1e18;
				if(f[ec]==0) cw=w[ec];
				if(f[ec]==1) cw=-w[ec];
				if(dist[c.se]>dist[x]+cw){
					dist[c.se]=dist[x]+cw;
					prv[c.se]=c.fi;
					pq.push({dist[c.se]-pot[c.se],c.se});
				}
			}
			else{
				ll cw=1e18;
				if(f[ec]==0) cw=w[ec];
				if(f[ec]==-1) cw=-w[ec];
				if(dist[c.se]>dist[x]+cw){
					dist[c.se]=dist[x]+cw;
					prv[c.se]=c.fi;
					pq.push({dist[c.se]-pot[c.se],c.se});
				}
			}
		}
	}
	int god=m;
	for(int i=1; i<=n ;i++){
		if(dist[i*m]<dist[god]) god=i*m;
	}
	if(dist[god]==k) return false;
	ans+=k-dist[god];
	while(god%m!=1){
		int ce=prv[god];
		god^=eu[ce/2]^ev[ce/2];
		if(ce%2) f[ce/2]--;
		else f[ce/2]++;
	}
	for(int i=1; i<=n*m ;i++) pot[i]=dist[i];
	return true;
}
void solve(){
	cin >> n >> m >> k;
	ec=0;ans=0;
	for(int i=1; i<=n*m ;i++) adj[i].clear();
	//init
	for(int i=1; i<=n ;i++){
		for(int j=1; j<m ;j++){
			cin >> r[i][j];
			add((i-1)*m+j,(i-1)*m+j+1,r[i][j]);
		}
	}
	for(int i=1; i<n ;i++){
		for(int j=1; j<=m ;j++){
			cin >> c[i][j];
			add((i-1)*m+j,i*m+j,c[i][j]);
		}
	}
	iu();
	while(jieun());
	cout << ans << '\n';
	for(int i=1; i<=n ;i++){
		for(int j=1; j<m ;j++){
			int u=(i-1)*m+j;
			int v=u+1;
			ll duh=max(pot[v]-pot[u],pot[u]-pot[v]);
			cout << max(r[i][j],duh) << ' ';
		}
		cout << '\n';
	}
	for(int i=1; i<n ;i++){
		for(int j=1; j<=m ;j++){
			int u=(i-1)*m+j;
			int v=i*m+j;
			ll duh=max(pot[v]-pot[u],pot[u]-pot[v]);
			cout << max(c[i][j],duh) << ' ';
		}
		cout << '\n';
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int t;cin >> t;while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5912kb

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
4 1 15 
7 1 9 
13 5 2 
3 6 1 2 
5 6 15 3 
4
3 2 
2 2 
3 3 
1 1 1 
2 2 2 

result:

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