QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65705#693. Hard Times for Your DataeyiigjknTL 5ms3712kbC++141.7kb2022-12-03 09:13:402022-12-03 09:13:42

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:13:42]
  • 评测
  • 测评结果:TL
  • 用时:5ms
  • 内存:3712kb
  • [2022-12-03 09:13:40]
  • 提交

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 || !minn) 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) continue;
		visE[i]=1;
		ll x=dfs(v,t,min(minn,ll(visE[rev[i]]?(c-f)/2:c-f)));
		visE[i]=0;
		edge[i].f+=x;edge[i^1].f-=x;
		edge[rev[i]].f+=x;edge[rev[i]^1].f-=x;
		ans+=x;minn-=x;
		if(!minn) 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: 4ms
memory: 3580kb

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
2 1 1
4 3 1
5 1 2

result:

ok OK

Test #2:

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

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 1 1
8 6 1
8 5 1
8 2 1
8 1 1
9 7 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: 5ms
memory: 3712kb

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
32 25 1
43 38 1
76 65 1
81 47 1
83 75 1
91 71 1
98 11 1
100 5 1
114 107 1
117 14 1
124 96 1
126 108 1
130 36 1
133 110 1
136 103 1
138 21 1
147 94 1
149 105 1
150 55 1
151 12 1
153 31 1
159 135 1
162 34 1
163 1 1
167 166 1
169 85 1
170 122 1
174 29 1
176 89 1
179 23 1
180 131 1
181 168 1
183 86 ...

result:

ok OK

Test #4:

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

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:

60
4 1 1
5 3 1
5 2 1
7 5 1
8 2 1
8 1 1
10 6 1
11 4 1
12 1 1
13 2 1
13 3 1
14 6 1
14 12 1
14 5 1
15 2 1
15 3 1
16 14 1
17 6 1
17 3 1
18 13 1
18 17 1
18 15 1
18 14 1
19 11 1
20 11 1
20 15 1
20 5 1
20 3 1
21 20 1
21 19 1
21 15 1
21 12 1
21 1 1
22 3 1
22 17 1
23 5 1
23 8 1
23 3 1
24 8 1
24 23 1
24 11 1
...

result:

ok OK

Test #5:

score: -100
Time Limit Exceeded

input:

8 28
4 4 3 6 3 5 5 6
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
2 3 1
2 4 1
2 5 1
2 6 1
2 7 1
2 8 1
3 4 1
3 5 1
3 6 1
3 7 1
3 8 1
4 5 1
4 6 1
4 7 1
4 8 1
5 6 1
5 7 1
5 8 1
6 7 1
6 8 1
7 8 1

output:


result: