
can anyone of you give me some hints to speed up my code ?
Moderator: Board moderators
Code: Select all
ACC... Thank you, Rio!
Can't believe I missed that ...
None of my test cases tested it ...
Code: Select all
computer[n] = 0;
Code: Select all
return Solve(n+1);
Code: Select all
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[300][300];
int task[300];
bool alpha[300];
char alloc[500];
bool seen[500];
bool bpm(int i)
{
int j;
for(j=0;j<10;j++)
if(a[i][j] && alloc[j]=='_')
{
alloc[j]=i+'A';
return true;
}
seen[i]=true;
for(j=0;j<10;j++)if(a[i][j] && (alloc[j]-'A')!=i && !seen[j])
{
if(bpm(alloc[j]-'A'))
{
alloc[j]=i+'A';
return true;
}
}
return false;
}
int main(void)
{
//freopen("259in.txt","r",stdin);
char s[1000];
int i,j,q;
while(gets(s))
{
memset(a,0,sizeof(a));
memset(alpha,false,sizeof(alpha));
memset(alloc,'_',sizeof(alloc));
if(strcmp(s,"")==0)
{
for(i=0;i<10;i++)
{
printf("%c",alloc[i]);
}
printf("\n");
continue;
}
int p=s[0]-'A';
task[p]=s[1]-'0';
alpha[p]=true;
for(i=3;s[i]!=';';i++)
{
q = s[i]-'0';
a[p][q]=1;
}
while(1)
{
if(!gets(s))
break;
if(strcmp(s,"")==0)
break;
p=s[0]-'A';
task[p]=s[1]-'0';
alpha[p]=true;
for(i=3;s[i]!=';';i++)
{
q=s[i]-'0';
a[p][q]=1;
}
}
int cnt=0;
int flag=1;
for(i=0;i<26;i++)if(alpha[i])
{
for(j=0;j<task[i];j++)
{
memset(seen,false,sizeof(seen));
if(!bpm(i))
{
flag=0;
break;
}
}
if(!flag)
break;
}
if(!flag)
{
printf("!\n");
}
else
{
for(i=0;i<10;i++)
{
printf("%c",alloc[i]);
}
printf("\n");
}
}
return 0;
}
Code: Select all
A1 13579;
B1 02468;
C1 01;
D1 02;
E1 1;
F1 3;
P1 4;
Q1 5;
Z1 7;
Code: Select all
CEDFPQBZ_A
Code: Select all
Accepted!
I was forget the flow on v->u , fnet on COMPUTER -> APPLICATION
Thanks.
Akter_Sust wrote:Any one can get runtime error because input may be like
A1 13579;
B1 a-123;
Handle this i got accepted other wise i got runtime error.
Code: Select all
#include <iostream>
#include <sstream>
using namespace std;
const int N = 38;
int C[N][N];
int F[N][N];
void clear()
{
memset(C,0,sizeof(C));
memset(F,0,sizeof(F));
}
const int s = 36;
const int t = 37;
const int inf = 1<<30;
int maxFlow()
{
int e[N], h[N];
memset(e,0,sizeof(e));
memset(h,0,sizeof(h));
h[s] = N;
for(int i = 0; i < N; ++i)
if(i != s)
{
F[s][i] = C[s][i];
F[i][s] = -F[s][i];
e[i] = F[s][i];
}
for(int i = 0, j = 0, sz = 0, maxh[N]; ; )
{
if(!sz)
{
for(i=0; i < N; ++i)
if(i != s && i != t && e[i])
{
if(!sz || h[i] > h[maxh[0]])
{
sz = 0;
maxh[sz++] = i;
}
else if(h[i] == h[maxh[0]])
maxh[sz++]=i;
}
}
if(!sz) break;
while(sz > 0)
{
bool pushed = false;
i = maxh[sz-1];
for(j=0; j < N; ++j)
if(C[i][j] - F[i][j] > 0 && h[i]==h[j]+1)
{
pushed = true;
int pf = min(C[i][j] - F[i][j], e[i]);
F[i][j] += pf; F[j][i] = -F[i][j];
e[i] -= pf; e[j] += pf;
if(e[i]==0)
{
sz--;
break;
}
}
if(!pushed)
{
h[i] = inf;
for(j=0; j < N; ++j)
if(C[i][j]-F[i][j] > 0 && h[j]+1 < h[i])
h[i] = h[j]+1;
if(h[maxh[sz-1]] < h[i])
{
sz=0;
break;
}
}
}
}
return e[t];
}
int main(int argv, char* arg[])
{
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
#endif
char buff[256];
char comps[256];
int expected_flow = 0;
clear();
while( 1 )
{
cin.getline( buff,256 );
if(buff[0]=='\0')
{
bool cant = false;
int flow = 0;
if(expected_flow <= 10)
{
for(int i=0; i < 10; ++i)
C[26+i][t] = 1;
flow = maxFlow();
}
else
cant = true;
if(expected_flow != flow || cant)
{
cout << '!' << endl;
}
else
{
for(int i = 26; i < s; ++i)
{
char c = '_';
if(F[i][t] > 0)
{
for(int k = 0; k < 26; ++k)
{
if(F[k][i] > 0)
{
c = 'A' + k;
break;
}
}
}
cout << c;
}
}
cout << endl;
if(cin.eof()) return 0;
clear();
expected_flow = 0;
continue;
}
istringstream iss(buff);
string ss;
iss >> ss;
if(ss[0] < 'A' || ss[0] > 'Z') continue;
int job = ss[0] - 'A';
int sz=0;
char ch = ' ';
while(1)
{
iss >> ch;
if(ch >= '0' && ch <= '9')
{
comps[sz++] = ch-'0'+26;
}
else
{
if(ch == ';')
{
for(int i=0;i<sz;++i)
C[job][comps[i]] = 1;
C[s][job] += ss[1] - '0';
expected_flow += ss[1] - '0';
}
break;
}
}
}
return 0;
}
K0stIa wrote: hello, guys.
I tired to submit problems on this site. I think i solved this problem and have problems with I/O. Can anybody see on my code below ? If u can give me some advises how to write I/O for the problem on this site i'll be very thankful for u, coz i like problems on this site and dislike the format input data on it.
I'm new one in this.
Code: Select all
while (gets(buf)) {
...
...
sscanf(buf, "%s%s", str1, str2);
...
...
while (gets(buf)) {
if (strlen(buf) <= 0)
break;
sscanf(buf, "%s%s", str1, str2);
...
...
}
...
...
}
K0stIa wrote:What u can say about my approach ?
It's one of the usual approaches in such kind problems. We add source & sink nodes, first we connect with jobs, second with computers. Edges capacity from source to jobs nodes are equal to amount of jobs, capacities from computer node to sink are equal one. So, in such graph max flow goes throw the matching edges for jobs & computers as we want. If it's no hard for u, please see my code. there is simple implementation of preflow-push algorithm for max-flow problem.
I don't understand what's wrong with my code...
p.s. IO part looks like ur...
and I have one request more, can u give me some advices about IO data in this site, it seems to me there are many problems which are not difficult but have such kind of Input data which have the most general form which are not contradict with condition.
And what can u say about next test ?
UVA toolkit returns "_A________"
does ur code provide this test ?