QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#123405#6113. Window Arrangementneko31662WA 1ms3432kbC++173.0kb2023-07-12 15:22:212023-07-12 15:24:53

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-12 15:24:53]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3432kb
  • [2023-07-12 15:22:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define vc vector
#define LL long long
#define pb push_back
#define eb emplace_back
#define db double
#define fi first
#define se second

#define INF 1e6
template<typename T,typename U>struct Graph{
	struct Edge{
	    int to;T w;U c;Edge(int to,T w,U c):to(to),w(w),c(c){}
    };vc<Edge> e;
    int n;vc<vc<int>> sn;Graph(int n):n(n),sn(n+1){}
	void add(int u,int v,T w,U c){sn[u].pb(e.size()),e.eb(v,w,c);}
};


template<typename T,typename U> pair<T,U> EK(Graph<T,U>& g,int s,int t,T init=INF){
    #define sgn(x) (x>0)//(x>1e-10)
    int n=g.n;vc<int> pre(n+1);vc<T> dis,inc;T flow=0;U cost=0;
    vc<U> high(n+1);
    vc<bool> vis;
    auto dijkstra=[&](){
        dis.assign(n+1,INF),dis[s]=0;
        inc.assign(n+1,0),inc[s]=init;
        vis.assign(n+1,0);
        priority_queue<pair<U,int>> q;q.push({0,s});
        while(!q.empty()){
            int u=q.top().second;q.pop();
            if(vis[u])continue;vis[u]=1;if(u==t)break;
            for(auto i:g.sn[u]){
                auto [v,w,c]=g.e[i];
                if(vis[v]||!sgn(w)||!sgn(dis[v]+high[v]-dis[u]-high[u]-c))continue;
                dis[v]=dis[u]+high[u]-high[v]+c,pre[v]=i,inc[v]=min(w,inc[u]);
                q.push({-dis[v],v});
            }
        }
        return sgn(inc[t]);
    };
    while(dijkstra()){
        for(int i=0;i<=n;++i)if(vis[i])high[i]+=dis[i]-dis[t];
        T res=inc[t];flow+=res,cost+=res*(-high[s]);
        for(int u=t,i;u!=s;u=g.e[i^1].to)
                i=pre[u],g.e[i].w-=res,g.e[i^1].w+=res;
    }
    return make_pair(flow,cost);
}



void solve(){
    int n,m;cin>>n>>m;
    vc<vc<LL>> p(n,vc<LL>(m));
    vc<vc<LL>> w(n,vc<LL>(m));
    for(int i=0;i<n;++i)for(int j=0;j<m;++j)cin>>p[i][j];
    for(int i=0;i<n;++i)for(int j=0;j<m;++j){
        cin>>w[i][j];
        w[i][j]-=(i==0)+(i==n-1)+(j==0)+(j==m-1);
        w[i][j]=max(w[i][j],0LL);
    }
    int s=n*m+(n+1)*m+n*(m+1),t=s+1;
    Graph<int,LL> g(t);
    auto add=[&](int u,int v,LL w,LL c){
        g.add(u,v,w,c),g.add(v,u,0,-c);
    };
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            int u=i*m+j;
            add(s,u,w[i][j],0);
            add(u,n*m+i*m+j,1,0);
            add(u,n*m+(i+1)*m+j,1,0);
            add(u,n*m+(n+1)*m+i*(m+1)+j,1,0);
            add(u,n*m+(n+1)*m+i*(m+1)+j+1,1,0);
        }
    }
    for(int i=0;i<n+1;++i){
        for(int j=0;j<m;++j){
            int u=n*m+i*m+j;
//            add(u,t,1,0);
            if(i>0&&i<n){
                add(u,t,1,p[i-1][j]*p[i][j]);
            }
        }
    }
    for(int i=0;i<n;++i){
        for(int j=0;j<m+1;++j){
            int u=n*m+(n+1)*m+i*(m+1)+j;
//            add(u,t,1,0);
            if(j>0&&j<m){
                add(u,t,1,p[i][j-1]*p[i][j]);
            }
        }
    }
    auto [flow,cost]=EK(g,s,t);
    cout<<cost;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    while(t--)solve();
    return 0;
}
/*
1
7
3 2 4 2 5 3 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 3
1 7 10
7 2 8
7 9 10
4 6 4
3 3 3
3 2 4
4 3 4
2 2 3

output:

678

result:

wrong answer 1st numbers differ - expected: '178', found: '678'