提出 #486936


ソースコード 拡げる

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<utility>
#include<algorithm>
#define E 1010
#define V 110
using namespace std;
struct frac{
	long long n;
	long long d;
	frac(long long _n=0,long long _d=1):n(_n),d(_d){long long g=gcd(n,d); n/=g; d/=g;}
	long long gcd(long long a,long long b)const{
		if(a<0) a=-a;
		if(b<0) b=-b;
		while(b){
			a%=b;
			a^=b;
			b^=a;
			a^=b;
		}
		return a;
	}
	long long int_part()const{return n>=0?n/d:(n-d+1)/d;}
	long long frac_part()const{return n%d>=0?n%d:n%d+d;}
	frac operator+(frac rhs)const{
		long long g=gcd(d,rhs.d);
		return frac(n*(rhs.d/g)+rhs.n*(d/g),d/g*rhs.d);
	}
	frac operator-(frac rhs)const{return operator+(frac(-rhs.n,rhs.d));}
	frac operator*(frac rhs)const{
		long long g1=gcd(n,rhs.d),g2=gcd(rhs.n,d);
		return frac((n/g1)*(rhs.n/g2),(d/g2)*(rhs.d/g1));
	}
	bool less(frac rhs)const{
		if(int_part()<rhs.int_part()) return true;
		else if(int_part()>rhs.int_part()) return false;
		if(!frac_part()||!rhs.frac_part()) return rhs.frac_part();
		return frac(rhs.d,rhs.frac_part()).less(frac(d,frac_part()));
	}
	bool operator<(long long x)const{return int_part()<x;}
	bool operator>(long long x)const{return int_part()>x||int_part()==x&&frac_part();}
	frac sq()const{return (*this)*(*this);}
};
int to[E<<1],w[E<<1],c[E<<1],nxt[E<<1],src,dst,cnt;
int head[V],in[V],from[V],mf[V];
long long dis[V];
void init(int s,int t){
	src=s;
	dst=t;
	memset(head,-1,sizeof(head));
	cnt=0;
}
void add_edge(int u,int v,int _w,int _c){
	to[cnt]=v;
	to[cnt|1]=u;
	c[cnt]=_c;
	c[cnt|1]=0;
	w[cnt]=_w;
	w[cnt|1]=-_w;
	nxt[cnt]=head[u];
	nxt[cnt|1]=head[v];
	head[u]=cnt;
	head[v]=cnt|1;
	cnt+=2;
}
bool sp(long long &flow,long long &cost){
	queue<int> q;
	memset(dis,0x3f,sizeof(dis));
	long long inf=dis[0];
	memset(in,0,sizeof(in));
	memset(from,0,sizeof(from));
	q.push(src);
	in[src]=1;
	dis[src]=0;
	mf[src]=inf;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		in[u]=0;
		for(int i=head[u];i!=-1;i=nxt[i]){
			if(!c[i]) continue;
			int v=to[i];
			if(dis[u]+w[i]<dis[v]){
				from[v]=i;
				dis[v]=dis[u]+w[i];
				mf[v]=min(mf[u],c[i]);
				if(!in[v]){
					q.push(v);
					in[v]=1;
				}
			}
		}
	}
	flow=mf[dst];
	cost=dis[dst];
	if(cost<inf){
		for(int u=dst;u!=src;u=to[from[u]^1]){
			c[from[u]]-=flow;
			c[from[u]^1]+=flow;
		}
		return true;
	}
	return false;
}
frac solve(){
	vector<long long> vf,vc;
	long long cost=0,flow=0,af,ac;
	while(sp(af,ac)){
		flow+=af;
		vf.push_back(af);
		vc.push_back(ac);
	}
	frac ans(flow*flow);
	for(int i=0;i<vf.size();i++){
		frac mp(flow-vc[i]*cost,vc[i]*vc[i]+1);
		if(mp<vf[i]&&mp>0){
			frac tmp=frac(cost*cost+flow*flow)+mp*frac(2*cost*vc[i]-2*flow)+mp*frac(flow-vc[i]*cost);
			if(tmp.less(ans)) ans=tmp;
		}
		flow-=vf[i];
		cost+=vc[i]*vf[i];
		if(ans>flow*flow+cost*cost){
			ans=frac(flow*flow+cost*cost);
		}
	}
	return ans;
}
int main(){
	int n,m,s,t,u,v,_w,_c;
	scanf("%d%d%d%d",&n,&m,&s,&t);
	init(s,t);
	while(m--){
		scanf("%d%d%d%d",&u,&v,&_c,&_w);
		add_edge(u,v,_w,_c);
	}
	frac ans=solve();
	printf("%lld/%lld\n",ans.n,ans.d);
	return 0;
}

提出情報

提出日時
問題 E - Cost Performance Flow
ユーザ NCTU_Thor
言語 C++ (GCC 4.9.2)
得点 100
コード長 3228 Byte
結果 AC
実行時間 41 ms
メモリ 932 KiB

コンパイルエラー

./Main.cpp: In function ‘int main()’:
./Main.cpp:132:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d",&n,&m,&s,&t);
                               ^
./Main.cpp:135:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d",&u,&v,&_c,&_w);
                                  ^

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 89
セット名 テストケース
All 00_sample_00, 00_sample_01, 00_sample_02, 10_00_SmallRandom_0010, 10_01_SmallRandom_0010, 10_02_SmallRandom_0010, 10_03_SmallRandom_0010, 10_04_SmallRandom_0010, 10_05_SmallRandom_0010, 10_06_SmallRandom_0010, 10_07_SmallRandom_0010, 10_08_SmallRandom_0010, 10_09_SmallRandom_0010, 10_10_SmallRandom_0010, 10_11_SmallRandom_0010, 10_12_SmallRandom_0010, 10_13_SmallRandom_0010, 10_14_SmallRandom_0010, 10_15_SmallRandom_0010, 10_16_SmallRandom_0010, 10_17_SmallRandom_0010, 10_18_SmallRandom_0010, 10_19_SmallRandom_0010, 15_00_SmallBipartite_0022, 15_01_SmallBipartite_0022, 15_02_SmallBipartite_0022, 15_03_SmallBipartite_0022, 15_04_SmallBipartite_0022, 15_05_SmallBipartite_0022, 15_06_SmallBipartite_0022, 15_07_SmallBipartite_0022, 15_08_SmallBipartite_0022, 15_09_SmallBipartite_0022, 15_10_SmallBipartite_0022, 15_11_SmallBipartite_0022, 15_12_SmallBipartite_0022, 15_13_SmallBipartite_0022, 15_14_SmallBipartite_0022, 15_15_SmallBipartite_0022, 15_16_SmallBipartite_0022, 15_17_SmallBipartite_0022, 15_18_SmallBipartite_0022, 15_19_SmallBipartite_0022, 20_00_LargeRandom_0100, 20_01_LargeRandom_0100, 20_02_LargeRandom_0100, 20_03_LargeRandom_0100, 20_04_LargeRandom_0100, 20_05_LargeRandom_0100, 20_06_LargeRandom_0100, 20_07_LargeRandom_0100, 20_08_LargeRandom_0100, 20_09_LargeRandom_0100, 20_10_LargeRandom_0100, 20_11_LargeRandom_0100, 20_12_LargeRandom_0100, 20_13_LargeRandom_0100, 20_14_LargeRandom_0100, 20_15_LargeRandom_0100, 20_16_LargeRandom_0100, 20_17_LargeRandom_0100, 20_18_LargeRandom_0100, 20_19_LargeRandom_0100, 25_00_LargeBipartite_0100, 25_01_LargeBipartite_0100, 25_02_LargeBipartite_0100, 25_03_LargeBipartite_0100, 25_04_LargeBipartite_0100, 25_05_LargeBipartite_0100, 25_06_LargeBipartite_0100, 25_07_LargeBipartite_0100, 25_08_LargeBipartite_0100, 25_09_LargeBipartite_0100, 25_10_LargeBipartite_0100, 25_11_LargeBipartite_0100, 25_12_LargeBipartite_0100, 25_13_LargeBipartite_0100, 25_14_LargeBipartite_0100, 25_15_LargeBipartite_0100, 25_16_LargeBipartite_0100, 25_17_LargeBipartite_0100, 25_18_LargeBipartite_0100, 25_19_LargeBipartite_0100, 30_00_CornerCase_0100, 30_01_CornerCase_0100, 30_02_CornerCase_0100, 40_00_MaxFlow_0100, 50_00_MaxCost_0100, 50_01_MaxCost_0100
ケース名 結果 実行時間 メモリ
00_sample_00 AC 25 ms 928 KiB
00_sample_01 AC 24 ms 928 KiB
00_sample_02 AC 25 ms 844 KiB
10_00_SmallRandom_0010 AC 26 ms 804 KiB
10_01_SmallRandom_0010 AC 25 ms 928 KiB
10_02_SmallRandom_0010 AC 26 ms 812 KiB
10_03_SmallRandom_0010 AC 24 ms 804 KiB
10_04_SmallRandom_0010 AC 26 ms 804 KiB
10_05_SmallRandom_0010 AC 25 ms 928 KiB
10_06_SmallRandom_0010 AC 25 ms 924 KiB
10_07_SmallRandom_0010 AC 24 ms 928 KiB
10_08_SmallRandom_0010 AC 27 ms 800 KiB
10_09_SmallRandom_0010 AC 25 ms 808 KiB
10_10_SmallRandom_0010 AC 25 ms 928 KiB
10_11_SmallRandom_0010 AC 25 ms 800 KiB
10_12_SmallRandom_0010 AC 24 ms 796 KiB
10_13_SmallRandom_0010 AC 26 ms 800 KiB
10_14_SmallRandom_0010 AC 26 ms 808 KiB
10_15_SmallRandom_0010 AC 26 ms 800 KiB
10_16_SmallRandom_0010 AC 25 ms 800 KiB
10_17_SmallRandom_0010 AC 26 ms 924 KiB
10_18_SmallRandom_0010 AC 25 ms 928 KiB
10_19_SmallRandom_0010 AC 25 ms 928 KiB
15_00_SmallBipartite_0022 AC 24 ms 928 KiB
15_01_SmallBipartite_0022 AC 26 ms 924 KiB
15_02_SmallBipartite_0022 AC 24 ms 812 KiB
15_03_SmallBipartite_0022 AC 26 ms 804 KiB
15_04_SmallBipartite_0022 AC 26 ms 928 KiB
15_05_SmallBipartite_0022 AC 25 ms 924 KiB
15_06_SmallBipartite_0022 AC 24 ms 800 KiB
15_07_SmallBipartite_0022 AC 25 ms 924 KiB
15_08_SmallBipartite_0022 AC 25 ms 928 KiB
15_09_SmallBipartite_0022 AC 26 ms 800 KiB
15_10_SmallBipartite_0022 AC 26 ms 808 KiB
15_11_SmallBipartite_0022 AC 24 ms 808 KiB
15_12_SmallBipartite_0022 AC 26 ms 804 KiB
15_13_SmallBipartite_0022 AC 25 ms 928 KiB
15_14_SmallBipartite_0022 AC 26 ms 804 KiB
15_15_SmallBipartite_0022 AC 24 ms 928 KiB
15_16_SmallBipartite_0022 AC 28 ms 840 KiB
15_17_SmallBipartite_0022 AC 41 ms 684 KiB
15_18_SmallBipartite_0022 AC 28 ms 764 KiB
15_19_SmallBipartite_0022 AC 27 ms 928 KiB
20_00_LargeRandom_0100 AC 33 ms 768 KiB
20_01_LargeRandom_0100 AC 30 ms 928 KiB
20_02_LargeRandom_0100 AC 29 ms 800 KiB
20_03_LargeRandom_0100 AC 31 ms 800 KiB
20_04_LargeRandom_0100 AC 30 ms 800 KiB
20_05_LargeRandom_0100 AC 31 ms 924 KiB
20_06_LargeRandom_0100 AC 33 ms 804 KiB
20_07_LargeRandom_0100 AC 30 ms 804 KiB
20_08_LargeRandom_0100 AC 29 ms 796 KiB
20_09_LargeRandom_0100 AC 27 ms 800 KiB
20_10_LargeRandom_0100 AC 29 ms 804 KiB
20_11_LargeRandom_0100 AC 27 ms 796 KiB
20_12_LargeRandom_0100 AC 28 ms 928 KiB
20_13_LargeRandom_0100 AC 30 ms 796 KiB
20_14_LargeRandom_0100 AC 32 ms 800 KiB
20_15_LargeRandom_0100 AC 33 ms 920 KiB
20_16_LargeRandom_0100 AC 24 ms 800 KiB
20_17_LargeRandom_0100 AC 29 ms 800 KiB
20_18_LargeRandom_0100 AC 26 ms 800 KiB
20_19_LargeRandom_0100 AC 31 ms 932 KiB
25_00_LargeBipartite_0100 AC 25 ms 928 KiB
25_01_LargeBipartite_0100 AC 23 ms 924 KiB
25_02_LargeBipartite_0100 AC 25 ms 924 KiB
25_03_LargeBipartite_0100 AC 26 ms 916 KiB
25_04_LargeBipartite_0100 AC 25 ms 920 KiB
25_05_LargeBipartite_0100 AC 25 ms 804 KiB
25_06_LargeBipartite_0100 AC 25 ms 800 KiB
25_07_LargeBipartite_0100 AC 23 ms 736 KiB
25_08_LargeBipartite_0100 AC 23 ms 800 KiB
25_09_LargeBipartite_0100 AC 23 ms 796 KiB
25_10_LargeBipartite_0100 AC 23 ms 924 KiB
25_11_LargeBipartite_0100 AC 23 ms 796 KiB
25_12_LargeBipartite_0100 AC 25 ms 928 KiB
25_13_LargeBipartite_0100 AC 23 ms 800 KiB
25_14_LargeBipartite_0100 AC 25 ms 728 KiB
25_15_LargeBipartite_0100 AC 25 ms 728 KiB
25_16_LargeBipartite_0100 AC 25 ms 928 KiB
25_17_LargeBipartite_0100 AC 23 ms 800 KiB
25_18_LargeBipartite_0100 AC 27 ms 800 KiB
25_19_LargeBipartite_0100 AC 25 ms 920 KiB
30_00_CornerCase_0100 AC 24 ms 792 KiB
30_01_CornerCase_0100 AC 28 ms 920 KiB
30_02_CornerCase_0100 AC 29 ms 792 KiB
40_00_MaxFlow_0100 AC 28 ms 784 KiB
50_00_MaxCost_0100 AC 25 ms 924 KiB
50_01_MaxCost_0100 AC 26 ms 924 KiB