Page 2 of 3


Posted: Tue Jul 19, 2005 3:07 pm
by dip
Hi Jalal,
My accepted codes give the same result. Good Luck :D

10330 Power Transmission

Posted: Mon Feb 06, 2006 1:34 pm
by wook
I used Edmonds-Karp algorithm but WA.

to deal with the vertex capacity, i used two additional arrays:

vc : the vertex capacity of vertex u
vf : the current flow of vertex u

always vf <= vc

residual capacity of vertex is vc-vf then.

so, for every augumenting path P,
the flow is min{ every residual capacity of edges or vertex in P }

is this wrong?

I tried all of test data in previous board here,
but my program didn't gave the wrong one.

please, clarify me why this gave me WA,
or give me some tricky test datas.


Code: Select all

#include <stdio.h>
#include <vector>

using namespace std;
typedef vector<int> vi;

#define maxn 105
#define fmin(x, y) ( (x) > (y) ? (y) : (x) )

const int inf = 2147483647;
int V, E, s, t, B, D;
int c[maxn][maxn]; // capacity
int f[maxn][maxn];

int vc[maxn]; // vertex capacity
int vf[maxn]; // vertex flow

vi neigh_list[maxn];
int que[maxn];
int par[maxn];

bool input_graph()
	int i, x, y, z;
	if( scanf("%d", &V) != 1)
		return false;

	for(i=1; i<=V; ++i)
		scanf("%d", vc + i);

	scanf("%d", &E);
	for(i = 1; i <= V; ++ i)

	for(i = 0; i < E; ++ i)
		scanf("%d %d %d", &x, &y, &z);
		c[x][y] = z;


	// source and sink
	V += 2;
	s = V-1; t = V;
	vc[s] = vc[t] = inf;


	// multisource, multisink
	scanf("%d %d", &B, &D);
		scanf("%d", &x);
		c[s][x] = inf;

		scanf("%d", &y);
		c[y][t] = inf;

	memset(vf, 0, sizeof(vf));
	return true;

bool path()
	int front, rear;
	int u, v, i, l;
	int dis[maxn] = {0,};
	front = rear = 0;

	for(i=1; i<=V; ++i)
		dis[i] = inf;

	que[++rear] = s;
	dis[s] = 0;
	par[s] = 0;

	while(front != rear)
		u = que[++front];

		l = neigh_list[u].size();
		for(i=0; i<l; ++i)
			v = neigh_list[u][i];
			if(vc[v] - vf[v] <= 0)
			if(c[u][v] - f[u][v] <= 0)

			if(dis[u] + 1 < dis[v])
				dis[v] = dis[u] + 1;
				par[v] = u;
				que[++rear] = v;

	if(dis[t] < inf)
		return true;
	return false;

void edmonds_karp()
	int u, v;
	int min;

	while( path() )
		min = vc[t] - vf[t];
		for(u = par[t], v = t; v != s; u = par[u], v = par[v])
			min = fmin(min, c[u][v] - f[u][v]);
			min = fmin(min, vc[u] - vf[u]);
		vf[t] += min;
		for(u = par[t], v = t; v != s; u = par[u], v = par[v])
			f[u][v] += min;
			f[v][u] -= min;
			vf[u] += min;

	printf("%d\n", vf[t]);

int main()
	freopen("", "r", stdin);

	while( input_graph() )
		memset(f, 0, sizeof(f));
		memset(c, 0, sizeof(c));

	return 0;

Posted: Tue Feb 14, 2006 5:30 pm
by nymo
Hi, I am trying to solve this problem (10330: Power transmission) but i get WA. I tried all the previous I/O posted on board and it produced me correct answers. Any tricky case? Any good I/O will be a great help. Thanks...

Posted: Sat Feb 18, 2006 9:39 am
by sclo
an easier way to deal with the vertex capacity would be to split each vertex into 2. All incomming edges would enter the first part of the vertex, and all outgoing edges would leave the second part of the vertex, where the first and second part of the vertex is connected by an directed edge with the vertex capacity.

Posted: Fri Feb 24, 2006 5:14 am
by nymo
Hi sclo, I actully did the same thing you mentioned. I tried my code with different IOs and it produced the correct answer. But everytime I submit to the judge, it is a WA. Is there any special case or any trick? Can anybody post some IO so that I can check???

Posted: Fri May 12, 2006 3:40 pm
by serur
Hi fellows!

I guess you got AC by now, so I'm curious to know what had caused your WAs then?

Posted: Fri May 12, 2006 4:31 pm
by serur
Well guys, pray don't look upon me as a poor thing :) but I really have no notion why this stuff gives WA in 0.154 CPU.
I want your help badly :roll:

Posted: Sat May 13, 2006 4:00 pm
by nymo
Hi, a standard trick to deal with vertex capacity is vertex splitting. Say, for node a, all incoming flow enters a new node, a1 and all outgoing flow goes out from a new node, a2. the own capacity of the node a is now between (a1, a2).i.e. node a is simulated by node a1 and a2.

For this problem, a test case posted by liulike can be a great help.

Code: Select all

1 1
1 1

Code: Select all

Though, problem statement says "Regulators connected with Barisal are not connected with Dhaka.", there is also mentioned that, "The input will start with a postive integer N ( 1<=N<=100 ) indicates the number of regulators", so, cases like N = 1 is valid case.
Anyway, I don't know if that is your problem, my cause of WA was :oops: funny :oops: , for the outgoing flows to the supersink, I simply use the node that was supposed to be used to incoming flows!!!

Posted: Sat May 13, 2006 4:37 pm
by serur
Hi nymo!

Thank you for reply.
For your test both of my codes gave correct answer. Here's my code that is made as you said: regulator number t becomes a directed edge between vertices t and (t+n), supersink is vertex no.2*n+1, supersource is 0.
This one worked 0,178 CPU, but,again,I don't know :( It's a pity that OJ's responses are not like that of Saratov OJ.


Posted: Sat May 13, 2006 8:58 pm
by serur
With similar algo I got AC 820-"Internet Bandwidth".
What the crucial difference between this one and that?
Please help!

Posted: Sun May 14, 2006 7:19 am
by nymo
Hi Serur, I submitted your code with different changes and everytime I got WA. I used your maxflow code, just tweaking input part. You initialized your residual matrix like this:

Code: Select all

and in the bfs part, you use

Code: Select all

         if(parents[y]==UNKNOWN && residual[x][y]>0){ 
Suppose, in the first time, you got a flow from v1 to v2(they are adjacent, c[v1][v2] > 0). But for every next time, you may have to find a path in the residual network which may run from v2 to v1, ain't it? As you have initialized your residual network, residual[v2][v1] = 0. So, your bfs will not find that path. I am not sure if it is indeed a problem, anyway, your input part is almost similar to mine but maxflow coding is somewhat different.

Posted: Sun May 14, 2006 8:37 am
by serur
Thank you nymo!

Look at this:

Code: Select all

You see what I have added? Only one line!
You were right - the problem is really in my residuals!

Thank you :)
Keep posting!
Have a nice weekend!

Posted: Sun May 14, 2006 8:41 am
by serur
Well, my next concern is biparite/complete matching. :)
Or I heard about something like mincost_maxflow...

Posted: Thu Aug 17, 2006 10:44 pm
by 898989
I think this problem is too strange.
I try ford with dijkstra and got ac in 0.213
I try ford with bfs and got TLE(opposite to my expections)
also Edmonds-Karp Algorithm with bellmanford give TLE

I think the codes that get TLE has problems with infinite loops not the efficieny of the code itself, so i must think about a bug in these implementaion. I found more than 400 submission got TLE

10330 - Getting WA. Plz Hlp

Posted: Thu Aug 17, 2006 11:02 pm
by shihabrc
Hi all,

I'm getting WA in this prob. I've used EKarp MaxFlow to solve it. I've splitted each vertex(other than source and sink) in two and have places a edge between them having capacity equal to the given node capacity. I've tested my code with several inputs (also the ones given in prev posts), and it gave correct result.
Plz some1 hlp.(I/O, suggestions, anything...)

Code: Select all

//Cut after AC
Got AC at last