Pagini recente » Istoria paginii runda/preoji_cl11_12_lspvs/clasament | Istoria paginii runda/cls_11_12_simulare_oji | Monitorul de evaluare | Istoria paginii agora-finala/clasament | Cod sursa (job #432849)
Cod sursa(job #432849)
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
#define buffsz 65536
#define MM 16505
#define NM 355
#define inf 2000000000
int vec[MM], cst[MM], start[NM], stop[NM], gi[NM];
int N, M, S, D;
FILE *fin;
char buff[buffsz];
int unde, refuri;
int C[NM][NM], F[NM][NM];
long long fmcm;
inline void refresh()
{
int hm = fread (buff, 1, buffsz, fin);
if (hm < buffsz) buff[hm] = 0;
unde = 0;
++refuri;
}
inline int cifra(char ch)
{
if(ch >= '0' && ch <= '9') return 1;
return 0;
}
inline int semn(char ch)
{
if(ch == '-') return 1;
return 0;
}
int readnr()
{
int ans = 0, minus = 0;
one:
while ((unde < buffsz) && (!cifra(buff[unde])) && (!semn(buff[unde])) ) ++unde;
if (unde == buffsz)
{
refresh();
goto one;
}
if (semn(buff[unde]))
{
minus = 1;
++unde;
}
two:
while ((unde < buffsz) && (cifra(buff[unde])))
{
ans = ans*10 + buff[unde] - '0';
++unde;
}
if (unde == buffsz)
{
refresh();
goto two;
}
if (minus) ans *= (-1);
return ans;
}
int BLF()
{
int DMIN[NM], FAT[NM], CD[5*NM];
bool IN[NM];
for (int i = 0; i <= N; ++i)
{
DMIN[i] = inf;
FAT[i] = 0;
IN[i] = 0;
}
int st = 0, dr = 0;
DMIN[S] = 0;
CD[st] = S;
IN[S] = 1;
while(st <= dr)
{
int nod = CD[st%NM];
IN[nod] = 0;
++st;
if (IN[FAT[nod]]) continue;
for (int i = start[nod]; i <= stop[nod]; ++i)
{
int nnod = vec[i];
int cost = cst[i];
if (C[nod][nnod] - F[nod][nnod] <= 0) continue;
if (DMIN[nnod] > DMIN[nod] + cost)
{
DMIN[nnod] = DMIN[nod] + cost;
FAT[nnod] = nod;
if (!IN[nnod])
{
++dr;
CD[dr%NM] = nnod;
IN[nnod] = 1;
}
}
}
}
if (DMIN[D] == inf) return 0;
int fc = inf;
int nod = D;
while (FAT[nod])
{
int fat = FAT[nod];
fc = min (fc, C[fat][nod] - F[fat][nod]);
nod = fat;
}
fmcm += (long long) fc * DMIN[D];
nod = D;
while (FAT[nod])
{
int fat = FAT[nod];
F[fat][nod] += fc;
F[nod][fat] -= fc;
nod = fat;
}
return 1;
}
int main()
{
int clk_start=clock();
int a, b, c, d;
//freopen ("fmcm.in", "r", stdin);
freopen ("fmcm.out", "w", stdout);
//annoying stuff
fin = fopen ("fmcm.in", "r");
refresh();
N = readnr();
M = readnr();
S = readnr();
D = readnr();
for (int i = 1; i <= M; ++i)
{
a = readnr();
b = readnr();
c = readnr();
d = readnr();
++gi[a];
++gi[b];
}
fclose(fin);
int unde_bagi = 1;
for (int i = 1; i <= N; ++i)
{
start[i] = unde_bagi;
unde_bagi += gi[i];
stop[i] = unde_bagi-1;
}
memset (gi, 0, sizeof(gi));
fin = fopen ("fmcm.in", "r");
refresh();
N = readnr();
M = readnr();
S = readnr();
D = readnr();
for (int i = 1; i <= M; ++i)
{
a = readnr();
b = readnr();
c = readnr();
d = readnr();
C[a][b]+=c;
int unde_a = start[a] + gi[a];
int unde_b = start[b] + gi[b];
vec[unde_a] = b;
cst[unde_a] = d;
vec[unde_b] = a;
cst[unde_b] = -d;
++gi[a];
++gi[b];
}
fclose(fin);
/*
for (int i = 1; i <= N; ++i)
{
printf ("%d-->", i);
for (int j = start[i]; j <= stop[i]; ++j) printf ("%d ", vec[j]);
printf ("\n");
}
*/
while(BLF());
printf ("%lld", fmcm);
int clk_stop=clock();
//printf ("\n%lf", (double)(clk_stop-clk_start)/CLOCKS_PER_SEC);
return 0;
}