QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#409814#2208. FlowKevin5307AC ✓102ms4288kbC++231.7kb2024-05-12 18:48:152024-05-12 18:51:04

Judging History

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

  • [2024-05-12 18:51:04]
  • 评测
  • 测评结果:AC
  • 用时:102ms
  • 内存:4288kb
  • [2024-05-12 18:48:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int u[20020],v[20020],w[20020],c[20020],tot,deg[2020];
vector<int> G[2020];
void addEdge(int from,int to,int capacity,int cost)
{
	u[tot]=from;
	v[tot]=to;
	w[tot]=capacity;
	c[tot]=cost;
	G[from].push_back(tot++);
	u[tot]=to;
	v[tot]=from;
	w[tot]=0;
	c[tot]=-cost;
	G[to].push_back(tot++);
}
int dist[2020],head[2020];
bool inque[2020],vis[2020];
int cost;
bool spfa(int s,int t)
{
	memset(dist,0x3f,sizeof(dist));
	memset(inque,0,sizeof(inque));
	dist[s]=0;
	inque[s]=1;
	queue<int> q;
	q.push(s);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		inque[x]=0;
		for(auto e:G[x])
			if(dist[x]+c[e]<dist[v[e]]&&w[e])
			{
				dist[v[e]]=dist[x]+c[e];
				if(!inque[v[e]])
				{
					inque[v[e]]=1;
					q.push(v[e]);
				}
			}
	}
	return (dist[t]!=0x3f3f3f3f);
}
int dfs(int u,int t,int flow)
{
	if(u==t) return flow;
	vis[u]=1;
	int ret=0;
	for(;head[u]<G[u].size()&&flow;head[u]++)
	{
		int e=G[u][head[u]];
		if(!vis[v[e]]&&dist[v[e]]==dist[u]+c[e]&&w[e])
		{
			int aug=dfs(v[e],t,min(flow,w[e]));
			flow-=aug;
			ret+=aug;
			w[e]-=aug;
			w[e^1]+=aug;
			cost+=c[e]*aug;
			if(!flow)
			{
				vis[u]=0;
				return ret;
			}
		}
	}
	vis[u]=0;
	return ret;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m;
	cin>>n>>m;
	int sum=0;
	for(int i=1;i<=m;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		sum+=w;
		deg[u]+=w;
		deg[v]-=w;
		addEdge(u,v,w,1);
	}
	for(int i=1;i<=n;i++)
		if(deg[i]>0)
			addEdge(0,i,deg[i],0);
		else
			addEdge(i,n+1,-deg[i],0);
	while(spfa(0,n+1))
	{
		memset(head,0,sizeof(head));
		while(dfs(0,n+1,0x3f3f3f3f));
	}
	cout<<sum-cost<<endl;
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 72ms
memory: 3988kb

Test #2:

score: 0
Accepted
time: 24ms
memory: 4188kb

Test #3:

score: 0
Accepted
time: 37ms
memory: 4224kb

Test #4:

score: 0
Accepted
time: 40ms
memory: 4040kb

Test #5:

score: 0
Accepted
time: 57ms
memory: 4220kb

Test #6:

score: 0
Accepted
time: 69ms
memory: 3900kb

Test #7:

score: 0
Accepted
time: 87ms
memory: 4036kb

Test #8:

score: 0
Accepted
time: 37ms
memory: 3896kb

Test #9:

score: 0
Accepted
time: 49ms
memory: 4224kb

Test #10:

score: 0
Accepted
time: 64ms
memory: 3996kb

Test #11:

score: 0
Accepted
time: 1ms
memory: 3776kb

Test #12:

score: 0
Accepted
time: 57ms
memory: 3964kb

Test #13:

score: 0
Accepted
time: 71ms
memory: 4028kb

Test #14:

score: 0
Accepted
time: 77ms
memory: 4040kb

Test #15:

score: 0
Accepted
time: 14ms
memory: 3924kb

Test #16:

score: 0
Accepted
time: 37ms
memory: 3892kb

Test #17:

score: 0
Accepted
time: 43ms
memory: 3976kb

Test #18:

score: 0
Accepted
time: 10ms
memory: 3864kb

Test #19:

score: 0
Accepted
time: 70ms
memory: 4048kb

Test #20:

score: 0
Accepted
time: 0ms
memory: 3760kb

Test #21:

score: 0
Accepted
time: 61ms
memory: 4004kb

Test #22:

score: 0
Accepted
time: 51ms
memory: 4204kb

Test #23:

score: 0
Accepted
time: 55ms
memory: 3956kb

Test #24:

score: 0
Accepted
time: 58ms
memory: 4240kb

Test #25:

score: 0
Accepted
time: 61ms
memory: 4220kb

Test #26:

score: 0
Accepted
time: 56ms
memory: 3936kb

Test #27:

score: 0
Accepted
time: 22ms
memory: 3872kb

Test #28:

score: 0
Accepted
time: 21ms
memory: 3864kb

Test #29:

score: 0
Accepted
time: 74ms
memory: 3908kb

Test #30:

score: 0
Accepted
time: 84ms
memory: 4008kb

Test #31:

score: 0
Accepted
time: 53ms
memory: 4208kb

Test #32:

score: 0
Accepted
time: 93ms
memory: 4276kb

Test #33:

score: 0
Accepted
time: 63ms
memory: 4240kb

Test #34:

score: 0
Accepted
time: 83ms
memory: 4036kb

Test #35:

score: 0
Accepted
time: 39ms
memory: 4208kb

Test #36:

score: 0
Accepted
time: 49ms
memory: 4240kb

Test #37:

score: 0
Accepted
time: 61ms
memory: 4036kb

Test #38:

score: 0
Accepted
time: 61ms
memory: 4028kb

Test #39:

score: 0
Accepted
time: 44ms
memory: 3960kb

Test #40:

score: 0
Accepted
time: 60ms
memory: 3968kb

Test #41:

score: 0
Accepted
time: 23ms
memory: 3936kb

Test #42:

score: 0
Accepted
time: 30ms
memory: 3884kb

Test #43:

score: 0
Accepted
time: 43ms
memory: 4220kb

Test #44:

score: 0
Accepted
time: 53ms
memory: 4024kb

Test #45:

score: 0
Accepted
time: 67ms
memory: 4232kb

Test #46:

score: 0
Accepted
time: 86ms
memory: 4256kb

Test #47:

score: 0
Accepted
time: 55ms
memory: 3900kb

Test #48:

score: 0
Accepted
time: 66ms
memory: 4256kb

Test #49:

score: 0
Accepted
time: 69ms
memory: 3880kb

Test #50:

score: 0
Accepted
time: 59ms
memory: 4244kb

Test #51:

score: 0
Accepted
time: 24ms
memory: 3896kb

Test #52:

score: 0
Accepted
time: 14ms
memory: 3924kb

Test #53:

score: 0
Accepted
time: 4ms
memory: 3840kb

Test #54:

score: 0
Accepted
time: 64ms
memory: 4040kb

Test #55:

score: 0
Accepted
time: 2ms
memory: 4108kb

Test #56:

score: 0
Accepted
time: 73ms
memory: 3956kb

Test #57:

score: 0
Accepted
time: 25ms
memory: 4192kb

Test #58:

score: 0
Accepted
time: 59ms
memory: 3912kb

Test #59:

score: 0
Accepted
time: 75ms
memory: 4020kb

Test #60:

score: 0
Accepted
time: 63ms
memory: 4068kb

Test #61:

score: 0
Accepted
time: 73ms
memory: 3928kb

Test #62:

score: 0
Accepted
time: 54ms
memory: 4116kb

Test #63:

score: 0
Accepted
time: 49ms
memory: 3936kb

Test #64:

score: 0
Accepted
time: 12ms
memory: 3912kb

Test #65:

score: 0
Accepted
time: 67ms
memory: 4004kb

Test #66:

score: 0
Accepted
time: 62ms
memory: 4032kb

Test #67:

score: 0
Accepted
time: 12ms
memory: 3940kb

Test #68:

score: 0
Accepted
time: 50ms
memory: 3912kb

Test #69:

score: 0
Accepted
time: 39ms
memory: 4188kb

Test #70:

score: 0
Accepted
time: 38ms
memory: 3900kb

Test #71:

score: 0
Accepted
time: 2ms
memory: 3808kb

Test #72:

score: 0
Accepted
time: 21ms
memory: 3964kb

Test #73:

score: 0
Accepted
time: 47ms
memory: 4164kb

Test #74:

score: 0
Accepted
time: 38ms
memory: 3972kb

Test #75:

score: 0
Accepted
time: 34ms
memory: 3836kb

Test #76:

score: 0
Accepted
time: 43ms
memory: 3968kb

Test #77:

score: 0
Accepted
time: 46ms
memory: 3936kb

Test #78:

score: 0
Accepted
time: 62ms
memory: 4020kb

Test #79:

score: 0
Accepted
time: 75ms
memory: 3932kb

Test #80:

score: 0
Accepted
time: 64ms
memory: 3952kb

Test #81:

score: 0
Accepted
time: 46ms
memory: 3972kb

Test #82:

score: 0
Accepted
time: 85ms
memory: 3976kb

Test #83:

score: 0
Accepted
time: 58ms
memory: 4256kb

Test #84:

score: 0
Accepted
time: 60ms
memory: 4208kb

Test #85:

score: 0
Accepted
time: 59ms
memory: 4044kb

Test #86:

score: 0
Accepted
time: 60ms
memory: 4020kb

Test #87:

score: 0
Accepted
time: 1ms
memory: 3828kb

Test #88:

score: 0
Accepted
time: 51ms
memory: 3908kb

Test #89:

score: 0
Accepted
time: 24ms
memory: 3892kb

Test #90:

score: 0
Accepted
time: 74ms
memory: 4016kb

Test #91:

score: 0
Accepted
time: 42ms
memory: 3976kb

Test #92:

score: 0
Accepted
time: 2ms
memory: 3796kb

Test #93:

score: 0
Accepted
time: 42ms
memory: 3952kb

Test #94:

score: 0
Accepted
time: 74ms
memory: 4196kb

Test #95:

score: 0
Accepted
time: 51ms
memory: 4168kb

Test #96:

score: 0
Accepted
time: 12ms
memory: 3808kb

Test #97:

score: 0
Accepted
time: 57ms
memory: 4188kb

Test #98:

score: 0
Accepted
time: 74ms
memory: 3976kb

Test #99:

score: 0
Accepted
time: 3ms
memory: 3812kb

Test #100:

score: 0
Accepted
time: 53ms
memory: 3996kb

Test #101:

score: 0
Accepted
time: 65ms
memory: 4048kb

Test #102:

score: 0
Accepted
time: 61ms
memory: 3992kb

Test #103:

score: 0
Accepted
time: 20ms
memory: 3832kb

Test #104:

score: 0
Accepted
time: 82ms
memory: 3996kb

Test #105:

score: 0
Accepted
time: 55ms
memory: 3932kb

Test #106:

score: 0
Accepted
time: 71ms
memory: 4248kb

Test #107:

score: 0
Accepted
time: 15ms
memory: 4188kb

Test #108:

score: 0
Accepted
time: 78ms
memory: 4156kb

Test #109:

score: 0
Accepted
time: 34ms
memory: 3956kb

Test #110:

score: 0
Accepted
time: 55ms
memory: 3988kb

Test #111:

score: 0
Accepted
time: 42ms
memory: 3900kb

Test #112:

score: 0
Accepted
time: 47ms
memory: 4000kb

Test #113:

score: 0
Accepted
time: 95ms
memory: 4000kb

Test #114:

score: 0
Accepted
time: 62ms
memory: 4048kb

Test #115:

score: 0
Accepted
time: 76ms
memory: 4232kb

Test #116:

score: 0
Accepted
time: 11ms
memory: 3920kb

Test #117:

score: 0
Accepted
time: 47ms
memory: 3908kb

Test #118:

score: 0
Accepted
time: 24ms
memory: 3948kb

Test #119:

score: 0
Accepted
time: 64ms
memory: 3908kb

Test #120:

score: 0
Accepted
time: 74ms
memory: 4204kb

Test #121:

score: 0
Accepted
time: 60ms
memory: 3984kb

Test #122:

score: 0
Accepted
time: 80ms
memory: 4028kb

Test #123:

score: 0
Accepted
time: 48ms
memory: 3936kb

Test #124:

score: 0
Accepted
time: 64ms
memory: 4044kb

Test #125:

score: 0
Accepted
time: 59ms
memory: 4280kb

Test #126:

score: 0
Accepted
time: 53ms
memory: 3972kb

Test #127:

score: 0
Accepted
time: 1ms
memory: 3844kb

Test #128:

score: 0
Accepted
time: 66ms
memory: 4028kb

Test #129:

score: 0
Accepted
time: 98ms
memory: 4024kb

Test #130:

score: 0
Accepted
time: 2ms
memory: 3896kb

Test #131:

score: 0
Accepted
time: 63ms
memory: 4212kb

Test #132:

score: 0
Accepted
time: 20ms
memory: 3840kb

Test #133:

score: 0
Accepted
time: 52ms
memory: 4044kb

Test #134:

score: 0
Accepted
time: 46ms
memory: 4028kb

Test #135:

score: 0
Accepted
time: 70ms
memory: 3924kb

Test #136:

score: 0
Accepted
time: 60ms
memory: 4036kb

Test #137:

score: 0
Accepted
time: 37ms
memory: 3888kb

Test #138:

score: 0
Accepted
time: 61ms
memory: 4012kb

Test #139:

score: 0
Accepted
time: 59ms
memory: 4016kb

Test #140:

score: 0
Accepted
time: 89ms
memory: 4024kb

Test #141:

score: 0
Accepted
time: 64ms
memory: 4004kb

Test #142:

score: 0
Accepted
time: 98ms
memory: 3956kb

Test #143:

score: 0
Accepted
time: 76ms
memory: 4256kb

Test #144:

score: 0
Accepted
time: 67ms
memory: 3964kb

Test #145:

score: 0
Accepted
time: 69ms
memory: 3904kb

Test #146:

score: 0
Accepted
time: 41ms
memory: 3964kb

Test #147:

score: 0
Accepted
time: 54ms
memory: 4024kb

Test #148:

score: 0
Accepted
time: 62ms
memory: 3892kb

Test #149:

score: 0
Accepted
time: 61ms
memory: 4052kb

Test #150:

score: 0
Accepted
time: 19ms
memory: 3916kb

Test #151:

score: 0
Accepted
time: 53ms
memory: 4228kb

Test #152:

score: 0
Accepted
time: 74ms
memory: 3860kb

Test #153:

score: 0
Accepted
time: 69ms
memory: 3976kb

Test #154:

score: 0
Accepted
time: 36ms
memory: 4156kb

Test #155:

score: 0
Accepted
time: 102ms
memory: 4220kb

Test #156:

score: 0
Accepted
time: 50ms
memory: 4288kb

Test #157:

score: 0
Accepted
time: 19ms
memory: 3940kb

Test #158:

score: 0
Accepted
time: 83ms
memory: 3952kb

Test #159:

score: 0
Accepted
time: 51ms
memory: 4000kb

Test #160:

score: 0
Accepted
time: 26ms
memory: 3996kb

Test #161:

score: 0
Accepted
time: 34ms
memory: 3948kb

Test #162:

score: 0
Accepted
time: 26ms
memory: 3884kb

Test #163:

score: 0
Accepted
time: 19ms
memory: 4112kb

Test #164:

score: 0
Accepted
time: 88ms
memory: 3992kb

Test #165:

score: 0
Accepted
time: 20ms
memory: 3936kb

Test #166:

score: 0
Accepted
time: 62ms
memory: 4252kb

Test #167:

score: 0
Accepted
time: 46ms
memory: 4136kb

Test #168:

score: 0
Accepted
time: 68ms
memory: 4284kb

Test #169:

score: 0
Accepted
time: 72ms
memory: 3984kb

Test #170:

score: 0
Accepted
time: 73ms
memory: 3948kb

Test #171:

score: 0
Accepted
time: 78ms
memory: 3976kb

Test #172:

score: 0
Accepted
time: 36ms
memory: 3840kb

Test #173:

score: 0
Accepted
time: 63ms
memory: 3972kb

Test #174:

score: 0
Accepted
time: 57ms
memory: 3964kb

Test #175:

score: 0
Accepted
time: 48ms
memory: 4028kb

Test #176:

score: 0
Accepted
time: 87ms
memory: 4064kb

Test #177:

score: 0
Accepted
time: 40ms
memory: 3952kb

Test #178:

score: 0
Accepted
time: 58ms
memory: 3964kb

Test #179:

score: 0
Accepted
time: 46ms
memory: 3932kb

Test #180:

score: 0
Accepted
time: 10ms
memory: 3892kb

Test #181:

score: 0
Accepted
time: 75ms
memory: 3912kb

Test #182:

score: 0
Accepted
time: 44ms
memory: 3908kb

Test #183:

score: 0
Accepted
time: 41ms
memory: 4212kb

Test #184:

score: 0
Accepted
time: 74ms
memory: 3920kb

Test #185:

score: 0
Accepted
time: 66ms
memory: 3996kb

Test #186:

score: 0
Accepted
time: 77ms
memory: 3996kb