对拍正确提交错误的循环

这是我的:

编译成功

foo.cpp: In function 'void initedge(int, int, int, int)':
foo.cpp:20:19: warning: operation on 'num' may be undefined [-Wsequence-point]
测试数据 #0: Accepted, time = 0 ms, mem = 17616 KiB, score = 10
测试数据 #1: WrongAnswer, time = 15 ms, mem = 17616 KiB, score = 0
测试数据 #2: WrongAnswer, time = 15 ms, mem = 17620 KiB, score = 0
测试数据 #3: WrongAnswer, time = 15 ms, mem = 17616 KiB, score = 0
测试数据 #4: WrongAnswer, time = 0 ms, mem = 17620 KiB, score = 0
测试数据 #5: WrongAnswer, time = 15 ms, mem = 17616 KiB, score = 0
测试数据 #6: WrongAnswer, time = 15 ms, mem = 17624 KiB, score = 0
测试数据 #7: WrongAnswer, time = 46 ms, mem = 17624 KiB, score = 0
测试数据 #8: WrongAnswer, time = 62 ms, mem = 17620 KiB, score = 0
测试数据 #9: WrongAnswer, time = 78 ms, mem = 17620 KiB, score = 0
WrongAnswer, time = 261 ms, mem = 17624 KiB, score = 10

代码

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
const int oo=10000000,maxn=20000,maxm=600000;
int f[maxn],v[maxm];
int ff[maxn],r[maxn*4],head[maxm],next[maxm],vet2[maxm],vet[maxm],re[maxm],cost[maxm],pre[maxn],prep[maxn],pre2[maxn];
int n,m,num,s,t;
using namespace std;
void initedge(int x,int y,int z,int w)
{
next[++num]=head[x];
head[x]=num;
vet[num]=y;
vet2[num]=x;
v[num]=z;
cost[num]=w;
re[num]=++num;
next[num]=head[y];
head[y]=num;
vet[num]=x;
vet2[num]=y;
v[num]=0;
cost[num]=-w;
re[num]=num-1;
}
bool spfa()
{
int tou=0,wei=1;
memset(ff,0,sizeof(ff));
for (int i=1;i<=n;i++) f[i]=oo;
f[s]=0;
r[1]=s;
ff[s]=1;
while (tou<wei)
{
int p=head[r[++tou]];
ff[r[tou]]=0;
while (p>0)
{
if ((v[p])&&(f[vet[p]]>f[r[tou]]+cost[p]))
{
f[vet[p]]=f[r[tou]]+cost[p];
pre[vet[p]]=r[tou];
prep[vet[p]]=p;
if (!ff[vet[p]])
{
ff[vet[p]]=0;
r[++wei]=vet[p];
}
}
p=next[p];
}
}
if (f[t]==oo) return 0; return 1;
}
int main()
{
scanf("%d%d",&n,&m);
initedge(1,2,m,0);
int x;
for (int i=1;i<=n;i++)
{
scanf("%d",&x);
initedge(2,2*i+1,oo,0);
initedge(2*i+1,2*i+2,x,-oo);
initedge(2*i+2,2*n+3,oo,0);
}
for (int i=1;i<n;i++)
for (int j=i+1;j<=n;j++)
{
scanf("%d",&x);
if (x!=-1) initedge(i*2+2,j*2+1,oo,x);
}
n=2*n+3; s=1; t=n;
long long ans=0;
while (spfa())
{
int nowflow=oo;
for (int i=t;i!=s;i=pre[i]) nowflow=min(nowflow,v[prep[i]]);
for (int i=t;i!=s;i=pre[i])
{
int k1=prep[i],k2=re[k1];
v[k1]-=nowflow;
v[k2]+=nowflow;
ans+=nowflow*cost[k1];
}
}
while (ans>=oo) ans-=oo;
while (ans<0) ans+=oo;
cout<<ans;
return 0;
}

这是我网上找的ac标程(格式没办法):

编译成功

foo.cpp: In function 'int main()':
foo.cpp:80:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat]
测试数据 #0: Accepted, time = 15 ms, mem = 1352 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 1356 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 1352 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1352 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 1352 KiB, score = 10
测试数据 #6: Accepted, time = 78 ms, mem = 1356 KiB, score = 10
测试数据 #7: Accepted, time = 280 ms, mem = 1360 KiB, score = 10
测试数据 #8: Accepted, time = 452 ms, mem = 1360 KiB, score = 10
测试数据 #9: Accepted, time = 655 ms, mem = 1352 KiB, score = 10
Accepted, time = 1495 ms, mem = 1360 KiB, score = 100

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N=20010;
const int INF=1e9;
int to[N],next[N],q[N],c[N],f[N],w[N],map[N],dist[N],n,i,j,k,t,m;
int cnt[N],pre[N],fa[N];
bool flag[N];
int mlen,S,T;
int tot;
long long ans;
void add(int u,int v,int r,int p){
to[tot]=v;c[tot]=r;w[tot]=p;next[tot]=map[u];map[u]=tot++;
to[tot]=u;c[tot]=0;w[tot]=-p;next[tot]=map[v];map[v]=tot++;
//printf("%d %d\n",u,v);
}
void updata(){
int i,p;
i=pre[T]; mlen=INF; p=T;
while (i!=-1){
mlen=min(mlen,c[i]-f[i]);
i=pre[to[i^1]]; p=fa[p];
}
i=pre[T];p=T;
while (i!=-1){
f[i]+=mlen;f[i^1]-=mlen;
i=pre[to[i^1]];p=fa[p];
}
}
int spfa(){
int head,tail,now,node,p;
memset(dist,0x3f,sizeof(dist));
memset(flag,0,sizeof(flag));memset(pre,-1,sizeof(pre));
dist[S]=0;flag[S]=1;q[1]=S;head=0;tail=1;
while (head!=tail){
head=(head%N)+1; now=q[head];flag[now]=0;p=map[now];
while (p!=-1){
node=to[p];
if (c[p]>f[p]&&dist[node]>dist[now]+w[p]){
dist[node]=dist[now]+w[p];
pre[node]=p;
fa[node]=now;
if (flag[node]==0){
if (dist[node]<dist[q[(head%N)+1]]&&head!=tail){
q[head]=node;head--;if (head==0) head=N; flag[node]=1;
}
else{
tail=(tail%T)+1; q[tail]=node; flag[node]=1;
}
}
}
p=next[p];
}
}
return pre[T]!=-1;
}
int main(){
scanf("%d%d",&n,&m);
memset(map,-1,sizeof(map));
for (i=1;i<=n;i++) scanf("%d",&cnt[i]);
for (i=1;i<n;i++){
for (j=1;j<=n-i;j++){
scanf("%d",&k);
if (k!=-1)
add(i+j+n,i,INF,k);
}
}
T=2*n+1;S=2*n+2;
add(S,0,m,0);
for (i=1;i<=n;i++) add(0,i,INF,0);
for (i=1;i<=n;i++) add(i,i+n,cnt[i],-10000000);
for (i=1;i<=n;i++) add(i+n,T,INF,0);
while(spfa()){
updata();ans+=(long long)mlen*(long long)dist[T];
}
while (ans<0) ans+=10000000;
printf("%d",ans);
return 0;
}

4 条评论

  • 1

信息

ID
1213
难度
5
分类
图结构 | 网络流 点击显示
标签
递交数
626
已通过
205
通过率
33%
被复制
3
上传者