Mai intai trebuie sa te autentifici.
Cod sursa(job #2390897)
Utilizator | Data | 28 martie 2019 14:17:48 | |
---|---|---|---|
Problema | Flux maxim de cost minim | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 2.2 kb |
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define X 351
#define N 15000000
int n,m,s,t,x,y,u[X][X],v[X][X],r,z,e[X],d[X],f[X],p[X],a=-1,b;
vector<int> c[X];
char w[X];
queue<int> q;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> h;
char k[N];
int A()
{
int s=0,r=1;
a++;
if(k[a]=='-')
r=-1,a++;
for(;k[a]>='0'&&k[a]<='9';a++)
s=s*10+k[a]-'0';
return s*r;
}
void S(int x)
{
if(x<0)
x=-x,k[b++]='-';
int i,d=x>999999999?10:x>99999999?9:x>9999999?8:x>999999?7:x>99999?6:x>9999?5:x>999?4:x>99?3:x>9?2:1;
for(i=d-1;i>=0;x/=10,i--)
k[b+i]=x%10+48;
b+=d;
}
bool E()
{
int o,b,k,x,y,a;
for(memset(d,0x3f,sizeof(d)),d[s]=f[s]=0,h.push(make_pair(d[s],s));!h.empty();)
{
b=h.top().first,o=h.top().second,h.pop();
if(d[o]!=b)
continue;
for(vector<int>::iterator j=c[o].begin();j!=c[o].end();j++)
if(u[o][*j])
{
k=d[o]+v[o][*j]+e[o]-e[*j];
if(k<d[*j])
d[*j]=k,f[*j]=f[o]+v[o][*j],p[*j]=o,h.push(make_pair(d[*j],*j));
}
}
memcpy(e,f,sizeof(d));
if(d[t]==0x3f3f3f3f)
return false;
for(x=0x3f3f3f3f,a=0,y=t;y!=s;y=p[y])
x=min(x,u[p[y]][y]),a+=v[p[y]][y];
for(r+=x,z+=x*f[t],y=t;y!=s;y=p[y])
u[p[y]][y]-=x,u[y][p[y]]+=x;
return true;
}
bool B()
{
int i;
memset(e,0x3f,sizeof(e)),e[s]=0;
for(q.push(s),w[s]=1;!q.empty();q.pop())
{
i=q.front(),w[i]=0;
for(vector<int>::iterator j=c[i].begin();j!=c[i].end();j++)
if(u[i][*j])
{
if(e[i]+v[i][*j]>=e[*j])
continue;
e[*j]=e[i]+v[i][*j];
if(w[*j])
continue;
w[*j]=1,q.push(*j);
}
}
return e[t]==0x3f3f3f3f?false:true;
}
int main()
{
freopen("fmcm.in","r",stdin),freopen("fmcm.out","w",stdout),fread(k,1,N,stdin),n=A(),m=A(),s=A(),t=A();
while(m--)
x=A(),y=A(),c[x].push_back(y),c[y].push_back(x),u[x][y]=A(),v[x][y]=A(),v[y][x]=-v[x][y];
for(B();E(););
S(z),fwrite(k,1,b,stdout);
}