提出 #68924482


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
#include <queue>
const int maxn=1010;
typedef long long ll;
const ll inf=3e9;
#define MP make_pair
struct edge { int v,nxt; ll w,cost; } c[5000000];
ll d[maxn],base;
int SS,TT;
int head[maxn],edge_cnt,cur[maxn],S,T,node_cnt,q[maxn*maxn],hd,tl;
void addedge(int u,int v,ll w,ll cost) {
	c[++edge_cnt]=(edge){v,head[u],w,cost}; head[u]=edge_cnt;
	c[++edge_cnt]=(edge){u,head[v],0,-cost}; head[v]=edge_cnt;
}
void addedge(int u,int v,ll l,ll r,ll cost) {
    addedge(u,v,r-l,cost);
    d[v]+=l,d[u]-=l;
    base+=cost*l;
}
ll dep[maxn]; bool inq[maxn];
void init() { for (int i=0;i<=node_cnt;i++) head[i]=0; edge_cnt=1; }
bool bfs() {
    for (int i=0;i<=node_cnt;i++) dep[i]=inf,cur[i]=head[i],inq[i]=0;
	q[hd=tl=1]=S,dep[S]=0;
	while (hd<=tl) {
		int x=q[hd++]; inq[x]=0;
		for(int i=head[x];i;i=c[i].nxt) {
			if (c[i].w&&dep[c[i].v]>dep[x]+c[i].cost) {
				dep[c[i].v]=dep[x]+c[i].cost;
				if (!inq[c[i].v]) inq[c[i].v]=true,q[++tl]=c[i].v;
			} } }
	return dep[T]<inf; }
ll dfs(int x,ll flow) {
	if (x==T||!flow) return flow;
	inq[x]=1; int f=0,rf;
	for(int &i=cur[x];i;i=c[i].nxt) {
		if (!inq[c[i].v]&&dep[c[i].v]==dep[x]+c[i].cost
		&&(rf=dfs(c[i].v,min(flow,c[i].w)))) {
			flow-=rf,f+=rf;
			c[i].w-=rf,c[i^1].w+=rf;
			if(!flow) return f;
		} }
	return f;
}
pair<ll,ll> MCMF() {
	ll ans=0,res=0;
	while (bfs()) {
		ll tmp=dfs(S,inf);
        // if (dep[T]>0) break;
        ans+=tmp;
        res+=dep[T]*tmp;
	}
	return MP(ans,res+base);
}
void sol() {
	SS=++node_cnt,TT=++node_cnt;
	for(int i=1;i<=node_cnt-2;i++){
		if (d[i]>0) addedge(SS,i,d[i],0);
		else addedge(i,TT,-d[i],0);
		d[i]=0;
	}
	addedge(T,S,inf,0);
	S=SS,T=TT;
} // init() -> set cnt -> add_edge -> sol()
int n,m,a[maxn],b[maxn];

int main() {
    scanf("%d %d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=1;i<=n;i++) b[i]=a[i]-a[i-1];
    S=n+2,T=S+1;
    node_cnt=T;
    init();
    ll all=0;
    for (int i=1;i<=n;i++) {
        if (b[i]<0) addedge(S,i,-b[i],inf,0),all-=b[i];
        else addedge(i,T,0,b[i],0);
    }
    for (int i=1;i<=m;i++) {
        int x,y; scanf("%d %d",&x,&y);
        y++;
        addedge(x,y,inf,1);
    }
    addedge(n+1,T,inf,0);
    sol();
    pair<ll,ll> res=MCMF();
    if (all!=res.first) puts("-1");
    else printf("%lld\n",res.second);
    // printf("%lld %lld %lld\n",all,res.first,res.second);
    return 0;
}

提出情報

提出日時
問題 G - Increase to make it Increasing
ユーザ wygz
言語 C++ 20 (gcc 12.2)
得点 600
コード長 2491 Byte
結果 AC
実行時間 3 ms
メモリ 3984 KiB

コンパイルエラー

Main.cpp: In function ‘int main()’:
Main.cpp:69:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   69 |     scanf("%d %d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~
Main.cpp:70:33: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
      |                            ~~~~~^~~~~~~~~~~~
Main.cpp:81:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   81 |         int x,y; scanf("%d %d",&x,&y);
      |                  ~~~~~^~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 4
AC × 44
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 04_random4_04.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3828 KiB
00_sample_01.txt AC 1 ms 3628 KiB
00_sample_02.txt AC 1 ms 3744 KiB
00_sample_03.txt AC 1 ms 3696 KiB
01_random_00.txt AC 1 ms 3716 KiB
01_random_01.txt AC 1 ms 3728 KiB
01_random_02.txt AC 1 ms 3924 KiB
01_random_03.txt AC 1 ms 3744 KiB
01_random_04.txt AC 1 ms 3824 KiB
01_random_05.txt AC 1 ms 3772 KiB
02_random2_00.txt AC 2 ms 3712 KiB
02_random2_01.txt AC 2 ms 3836 KiB
02_random2_02.txt AC 2 ms 3648 KiB
02_random2_03.txt AC 1 ms 3944 KiB
02_random2_04.txt AC 3 ms 3732 KiB
02_random2_05.txt AC 2 ms 3956 KiB
02_random2_06.txt AC 1 ms 3896 KiB
02_random2_07.txt AC 3 ms 3728 KiB
02_random2_08.txt AC 3 ms 3984 KiB
02_random2_09.txt AC 1 ms 3708 KiB
02_random2_10.txt AC 2 ms 3628 KiB
02_random2_11.txt AC 2 ms 3844 KiB
02_random2_12.txt AC 3 ms 3860 KiB
02_random2_13.txt AC 2 ms 3840 KiB
02_random2_14.txt AC 3 ms 3912 KiB
02_random2_15.txt AC 2 ms 3820 KiB
02_random2_16.txt AC 2 ms 3780 KiB
02_random2_17.txt AC 3 ms 3964 KiB
02_random2_18.txt AC 2 ms 3824 KiB
02_random2_19.txt AC 3 ms 3688 KiB
03_random3_00.txt AC 1 ms 3688 KiB
03_random3_01.txt AC 1 ms 3964 KiB
03_random3_02.txt AC 3 ms 3748 KiB
03_random3_03.txt AC 2 ms 3728 KiB
04_random4_00.txt AC 3 ms 3716 KiB
04_random4_01.txt AC 3 ms 3720 KiB
04_random4_02.txt AC 3 ms 3720 KiB
04_random4_03.txt AC 3 ms 3848 KiB
04_random4_04.txt AC 3 ms 3588 KiB
05_handmade_00.txt AC 1 ms 3604 KiB
05_handmade_01.txt AC 1 ms 3856 KiB
05_handmade_02.txt AC 2 ms 3752 KiB
05_handmade_03.txt AC 2 ms 3984 KiB
05_handmade_04.txt AC 2 ms 3920 KiB