QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#643270#7737. Extending DistanceskulkerWA 0ms22000kbC++147.0kb2024-10-15 20:14:352024-10-15 20:14:36

Judging History

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

  • [2024-10-15 20:14:36]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:22000kb
  • [2024-10-15 20:14:35]
  • 提交

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-1)+y;
}
int find_x(int x){
    return (x-1)/(m-1)+1;
}
int find_y(int y){
    int x=(m-1)*(find_x(y)-1);
    if(x==y) return m-1;
    else return y-x;
}

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 ;
         //   if(from[i-1]==s||to[i-1]==s||from[i-1]==t||to[i-1]==t) continue ;
  //          cout<<"Edi="<<ed[i]<<'\n';
            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<<"from="<<from[i-1]<<" to="<<to[i-1]<<'\n';
//            cout<<"x1="<<x1<<" x2="<<x2<<" y1="<<y1<<" y2="<<y2<<'\n';
//            cout<<'\n';
            if(x1==x2){
                R[x1+1][y1]+=ed[i];
            }
            else C[x1][y1+1]+=ed[i];
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m-1;j++){
                cout<<R[i][j]<<" ";
            }
            cout<<'\n';
        }
        for(int i=1;i<=n-1;i++){
            for(int j=1;j<=m;j++){
                cout<<C[i][j]<<" ";
            }
            cout<<'\n';
        }
    }
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 22000kb

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 
7 1 14 
13 4 2 
3 8 1 2 
5 2 15 4 
4
1 1 
2 5 
3 3 
1 1 2 
2 2 2 

result:

wrong answer The length of shortest path shoult extend exactly K. (test case 1)