QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#188665 | #3588. Hamilton - The Musical | Zhou_JK | TL | 1ms | 5860kb | C++23 | 2.8kb | 2023-09-26 10:43:46 | 2023-09-26 10:43:46 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=505,M=N*N*10;
const long long INF=4557430888798830399;
int n;
int l[N][N];
struct Edge
{
int to,next;
int cost;
long long flow;
}edge[M*2];
int cur[N],head[N],cnt=1;
int s,t;
void add_edge(int u,int v,int c,int f)
{
cnt++;
edge[cnt].to=v;
edge[cnt].cost=c;
edge[cnt].flow=f;
edge[cnt].next=head[u];
head[u]=cnt;
return;
}
void add(int u,int v,int c,int f)
{
add_edge(u,v,c,f);
add_edge(v,u,-c,0);
return;
}
long long dis[N];
bool spfa()
{
static bool vis[N];
memset(vis,false,sizeof(vis));
memset(dis,63,sizeof(dis));
queue<int>q;
vis[s]=true;
dis[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].flow<=0) continue;
if(dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
return dis[t]!=INF;
}
bool book[N];
pair<long long,long long> dfs(int u,long long flow)
{
if(u==t||flow==0) return make_pair(flow,0);
book[u]=true;
long long used=0,res=0;
for(int &i=cur[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(book[v]||dis[v]!=dis[u]+edge[i].cost||edge[i].flow<=0) continue;
pair<long long,long long>t=dfs(v,min(flow,edge[i].flow));
long long now=t.first;
res+=t.second+now*edge[i].cost;
flow-=now;
edge[i].flow-=now;
edge[i^1].flow+=now;
used+=now;
if(flow==0) break;
}
book[u]=false;
return make_pair(used,res);
}
pair<long long,long long> dinic()
{
long long ans=0,Min=0;
while(spfa())
{
memcpy(cur,head,sizeof(head));
pair<long long,long long> res=dfs(s,INF);
ans+=res.first,Min+=res.second;
}
return make_pair(ans,Min);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>l[i][j];
s=n+1,t=n+2;
for(int i=2;i<=n;i+=2)
{
if(i==n)
{
for(int j=1;j<=n;j+=2)
add(i,j,l[i][j],1);
add(s,i,0,1);
}
else
{
for(int j=1;j<=n;j+=2)
add(i,j,l[i][j],1);
add(s,i,0,2);
}
}
for(int i=1;i<=n;i+=2)
add(i,t,0,2);
long long ans=dinic().second;
cout<<ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5860kb
input:
4 0 3 2 13 3 0 8 9 2 8 0 5 13 9 5 0
output:
16
result:
ok single line: '16'
Test #2:
score: -100
Time Limit Exceeded
input:
455 0 71154840 37432199 724743679 761809953 949789082 725973225 28455138 924138757 574256423 297516452 223475432 693485895 699629134 731875885 697643903 595490098 206757530 965177624 178416136 103223719 474785234 322984824 510200285 656185874 993023494 973542087 511171280 465648799 836547414 8145240...