QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65710#693. Hard Times for Your DataeyiigjknTL 779ms3812kbC++141.7kb2022-12-03 09:42:052022-12-03 09:42:06

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-03 09:42:06]
  • 评测
  • 测评结果:TL
  • 用时:779ms
  • 内存:3812kb
  • [2022-12-03 09:42:05]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll INF=1e18;
int rev[600010],head[1010],nxt[600010],tot=1;
bool visV[1010],visE[600010];
tuple<int,int,int> ans[200010];
mt19937 rnd;
struct Edge
{
	int v,c,f;
	Edge(){}
	Edge(int v,int c):v(v),c(c),f(0){}
}edge[600010];
void addedge(int u,int v,int c)
{
	nxt[++tot]=head[u];
	edge[tot]=Edge(v,c);
	head[u]=tot;
}
void addE(int u,int v,int c){addedge(u,v,c);addedge(v,u,0);}
ll dfs(int u,int t,ll minn)
{
	if(u==t) return minn;
	visV[u]=1;
	ll ans=0;
	vector<int> id;
	for(int i=head[u];i;i=nxt[i]) id.push_back(i);
	shuffle(id.begin(),id.end(),rnd);
	for(int i:id)
	{
		int v=edge[i].v,c=edge[i].c,f=edge[i].f;
		if(visV[v] || c-f<=(visE[rev[i]]?1:0)) continue;
		visE[i]=1;
		ll x=dfs(v,t,min(minn,ll(visE[rev[i]]?(c-f)/2:c-f)));
		visE[i]=0;
		if(!x) continue;
		edge[i].f+=x;edge[i^1].f-=x;
		edge[rev[i]].f+=x;edge[rev[i]^1].f-=x;
		ans=x;break;
	}
	visV[u]=0;
	return ans;
}
int main()
{
	int n,m,u,v,c,s,t;
	ll sum=0;
	cin>>n>>m;s=n+n+1;t=s+1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&c);sum+=c;
		rev[tot+1]=tot+3;rev[tot+2]=tot+4;
		rev[tot+3]=tot+1;rev[tot+4]=tot+2;
		addE(s,i,c);addE(i+n,t,c);
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d",&u,&v,&c);
		rev[tot+1]=tot+3;rev[tot+2]=tot+4;
		rev[tot+3]=tot+1;rev[tot+4]=tot+2;
		addE(u,v+n,c);addE(v,u+n,c);
	}
	sum/=2;
	while(sum) sum-=dfs(s,t,INF);
	tot=0;
	for(int u=1;u<=n;u++)
		for(int i=head[u];i;i=nxt[i])
			if((v=edge[i].v-n)<u && edge[i].f>0)
				ans[++tot]=make_tuple(u,v,edge[i].f);
	cout<<tot<<endl;
	for(int i=1;i<=tot;i++) printf("%d %d %d\n",get<0>(ans[i]),get<1>(ans[i]),get<2>(ans[i]));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3464kb

input:

5 6
3 1 1 1 2
1 2 1
1 4 1
2 3 2
2 4 1
3 4 1
1 5 3

output:

3
3 2 1
4 1 1
5 1 2

result:

ok OK

Test #2:

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

input:

10 30
5 6 1 3 5 4 2 5 3 6
1 2 1
1 4 1
1 7 1
1 8 1
1 10 1
2 3 1
2 5 1
2 6 1
2 8 1
2 10 1
4 5 1
4 10 1
5 6 1
5 8 1
5 9 1
6 8 1
6 9 1
7 10 1
8 10 1
9 10 1
6 7 1
3 9 1
4 7 1
7 9 1
3 4 1
3 7 1
8 9 1
3 8 1
6 10 1
3 5 1

output:

20
2 1 1
3 2 1
4 1 1
5 4 1
5 2 1
6 5 1
6 2 1
7 6 1
7 1 1
8 5 1
8 2 1
8 1 1
9 8 1
9 5 1
10 6 1
10 9 1
10 8 1
10 4 1
10 2 1
10 1 1

result:

ok OK

Test #3:

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

input:

500 1500
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

250
15 12 1
27 10 1
31 5 1
42 36 1
62 17 1
85 14 1
87 20 1
90 4 1
98 11 1
101 52 1
103 84 1
114 102 1
121 45 1
124 96 1
133 59 1
135 116 1
136 107 1
138 37 1
147 94 1
148 78 1
149 105 1
150 127 1
151 65 1
154 38 1
160 67 1
161 69 1
162 34 1
165 109 1
168 25 1
169 43 1
170 35 1
179 23 1
183 86 1
185 ...

result:

ok OK

Test #4:

score: -100
Time Limit Exceeded

input:

30 100
5 5 7 2 7 6 1 4 1 3 5 3 3 5 5 3 4 4 2 7 5 2 4 6 4 5 2 2 5 3
1 4 1
1 8 1
1 12 1
1 21 1
1 26 1
2 5 1
2 8 1
2 14 1
2 24 1
2 29 1
3 5 1
3 13 1
3 15 1
3 17 1
3 19 1
3 20 1
3 23 1
4 11 1
5 7 1
5 14 1
5 20 1
5 24 1
5 26 1
6 10 1
6 17 1
6 24 1
6 25 1
6 27 1
6 28 1
8 17 1
8 22 1
9 20 1
10 24 1
10 29 1...

output:


result: