QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#643332 | #7737. Extending Distance | skulker | WA | 4ms | 24196kb | C++14 | 6.9kb | 2024-10-15 20:44:52 | 2024-10-15 20:44:52 |
Judging History
answer
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
long long inf =0x3f3f3f3f;
int head[510000];//网络流边数一定记得开两倍
int nex[510000];
int from[510000];
long long val[510000];
long long ed[510000];
int book[510000];
int to[510000];
long long dis[510000];
int cur[510000];//当前弧优化,废边的下一条边
int R[510][510];
int C[510][510];
int n,m,s,t;
int tot=0;
long long maxflow=0;
long long mincost=0;
int has(int x,int y){
return (x-1)*m+y;
}
int find_x(int x){
return x/m+1;
}
int find_y(int y){
return y%m;
}
void add(int u,int v,int w,int c){
to[tot]=v;
nex[tot]=head[u];
head[u]=tot;
ed[tot]=w;
val[tot]=c;
from[tot]=u;
tot++;
}
bool spfa(){
queue<int> q;
for(int i=s;i<=t;i++){
cur[i]= head[i];
dis[i]=inf;
book[i]=0;
}
dis[s]=0;
q.push(s);
book[s]=1;
while(!q.empty()){
int x=q.front();
q.pop();
book[x]=0;
for(int i=head[x]; i!=-1;i=nex[i]){
//从0开始编号,!=-1
int y=to[i];
if(ed[i]>0&&dis[y]>dis[x]+val[i]){
dis[y]=dis[x]+val[i];
if(book[y]==0) {
q.push(y);
book[y]=1;
}
}
}
}
if(dis[t]>=inf) return 0;
else return 1;
}
long long dfs(int x,long long nowf){ // 现在还剩的流量nowf
if(nowf == 0 || x == t) {return nowf;} // 如果 已经没有流量接着往下走了 或者 已经到终点了,那么返回
long long sum=0;
book[x]=1;
for(int i=cur[x];i!=-1;i=nex[i]){
int y=to[i];
cur[x]=i;
if(book[y]==1) continue ;
if(dis[y]!=dis[x]+val[i]) continue ;
if(ed[i]==0) continue ;
long long f=dfs(y,min(nowf-sum,ed[i]));
if(f<=0) continue ;// 这样走下去到不了源点
mincost+=f*val[i];
ed[i]-=f;
ed[i^1]+=f;
sum=sum+f;
//从这里流出了多少流量往下
if(nowf-sum<=0)break;//可用流量nowf已经流完了,直接退出
}
book[x]=0;
return sum;
}
int q1[510][510],q2[510][510];
int main(){
int T=1;
cin>>T;
while(T--) {
int k;
cin >>n>>m>>k;
tot = 0;
s = 0, t = n * m*2+ 1;
int u, v, w;
for (int i = s; i <= t; i++) {
head[i] = -1;
}
int c;
for(int i=1;i<=n;i++)
for(int j=1;j<=m-1;j++){
cin>>w;
R[i][j]=w;
c=0;
if(i==1){
add(s,has(i,j),w,c);
add(has(i,j),s,0,-c);
add(has(i,j),s,w,c);
add(s,has(i,j),0,-c);
}
else if(i==n){
add(has(i-1,j),t,w,c);
add(t,has(i-1,j),0,-c);
add(t,has(i-1,j),w,c);
add(has(i-1,j),t,0,-c);
}
else {
add(has(i-1,j),has(i,j),w,c);
add(has(i,j),has(i-1,j),0,-c);
add(has(i,j),has(i-1,j),w,c);
add(has(i-1,j),has(i,j),0,-c);
}
}
for(int i=1;i<=n-1;i++)
for(int j=1;j<=m;j++){
cin>>w;
C[i][j]=w;
c=0;
if(j==1){
}
else if(j==m){
}
else {
// (i,j-1) -> (i,j)
add(has(i,j-1),has(i,j),w,c);
add(has(i,j),has(i,j-1),0,-c);
add(has(i,j),has(i,j-1),w,c);
add(has(i,j-1),has(i,j),0,-c);
}
}
// add(u,v,w,c);
// add(v,u,0,-c);
maxflow=0;
mincost=0;
while (spfa() == 1) {
int temp = dfs(s, inf);
while (temp > 0) {
maxflow += temp;
temp = dfs(s, inf);
}
}
// cout<<"maxflow1="<<maxflow<<'\n';
int now=tot;
for(int i=1;i<tot;i+=2){
ed[i]=0;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m-1;j++){
w=inf;
c=1;
if(i==1){
add(s,has(i,j),w,c);
add(has(i,j),s,0,-c);
add(has(i,j),s,w,c);
add(s,has(i,j),0,-c);
}
else if(i==n){
add(has(i-1,j),t,w,c);
add(t,has(i-1,j),0,-c);
add(t,has(i-1,j),w,c);
add(has(i-1,j),t,0,-c);
}
else {
add(has(i-1,j),has(i,j),w,c);
add(has(i,j),has(i-1,j),0,-c);
add(has(i,j),has(i-1,j),w,c);
add(has(i-1,j),has(i,j),0,-c);
}
}
for(int i=1;i<=n-1;i++)
for(int j=1;j<=m;j++){
w=inf;
c=1;
if(j==1){
}
else if(j==m){
}
else {
// (i,j-1) -> (i,j)
add(has(i,j-1),has(i,j),w,c);
add(has(i,j),has(i,j-1),0,-c);
add(has(i,j),has(i,j-1),w,c);
add(has(i,j-1),has(i,j),0,-c);
}
}
// cout<<"maxflow="<<maxflow<<" k="<<k<<'\n';
t++;
head[t]=-1;
add(t-1,t,k,0);
add(t,t-1,0,0);
maxflow=0;
// cout<<"build over\n";
while (spfa() == 1) {
int temp = dfs(s, inf);
while (temp > 0) {
maxflow += temp;
temp = dfs(s, inf);
}
}
cout<<mincost<<'\n';
for(int i=1;i<=tot;i+=2){
if(ed[i]==0||val[i]!=-1) continue ;
int x1=find_x(from[i-1]),y1=find_y(from[i-1]),x2=find_x(to[i-1]),y2=find_y(to[i-1]);
// cout<<"ed="<<ed[i]<<'\n';
// cout<<"("<<x1<<","<<y1<<") -> ("<<x2<<", "<<y2<<")\n";
if(from[i-1]==s){
R[1][y2]+=ed[i];
}
else if(to[i-1]==t-1){
R[n][y2]+=ed[i];
}
else
if(x1==x2){
C[x1][y1+1]+=ed[i];
}
else R[x1+1][y1]+=ed[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m-1;j++){
cout<<R[i][j]<<" \n"[j==m-1];
}
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=m;j++){
cout<<C[i][j]<<" \n"[j==m];
}
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 24196kb
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 3 5 15 9 1 9 14 3 2 3 6 1 2 5 2 16 3 4 1 4 2 3 3 3 1 1 1 2 2 2
result:
wrong answer The length of shortest path shoult extend exactly K. (test case 1)