Mai intai trebuie sa te autentifici.
Cod sursa(job #1410195)
Utilizator | Data | 30 martie 2015 22:11:06 | |
---|---|---|---|
Problema | Cc | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 2.35 kb |
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
vector <int> G[205];
int N,M,E;
queue <int> Q;
const int INF = 1000000000;
int Cost[205][205],Father[205],Capacity[205][205],F[205][205],Distance[205],InQ[205];
int source,sink;
void Read()
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
int c;
scanf("%d",&c);
int x=i,y=j;
y+=N;
G[x].push_back(y);
G[y].push_back(x);
Cost[x][y]=c;
Cost[y][x]=-c;
Capacity[x][y]=1;
}
}
sink=N+N+1;
for(int i=1;i<=N;i++)
{
G[source].push_back(i);
G[i].push_back(source);
Capacity[source][i]=1;
}
for(int i=N+1;i<=N+N;i++)
{
G[sink].push_back(i);
G[i].push_back(sink);
Capacity[i][sink]=1;
}
}
void init()
{
memset(Father,-1,sizeof(Father));
for(int i=1;i<=N+N+1;i++)
Distance[i]=INF;
memset(InQ,0,sizeof(InQ));
Q.push(source);
InQ[source]=1;
Distance[source]=0;
}
bool bellmanFord()
{
init();
while(!Q.empty())
{
int x=Q.front();
InQ[x]=0;
Q.pop();
for(int i=0;i<G[x].size();i++)
{
int neighb=G[x][i];
if(Capacity[x][neighb]-F[x][neighb]>0 && Distance[x]+Cost[x][neighb]<Distance[neighb])
{
Distance[neighb]=Distance[x]+Cost[x][neighb];
Father[neighb]=x;
if(InQ[neighb]==0)
{
Q.push(neighb);
InQ[neighb]=1;
}
}
}
}
return Father[sink]!=-1;
}
void MaxFlow()
{
int MaxFlow=0,Min=0;
while(bellmanFord()==1)
{
int flow=N;
for(int node=sink;node!=source;node=Father[node])
flow=min(flow,Capacity[Father[node]][node]-F[Father[node]][node]);
for(int node=sink;node!=source;node=Father[node])
F[Father[node]][node]+=flow,F[node][Father[node]]-=flow;
MaxFlow+=flow;Min+=Distance[sink]*flow;
}
printf("%d\n",Min);
}
int main()
{
freopen("cc.in","r",stdin);
freopen("cc.out","w",stdout);
Read();
MaxFlow();
return 0;
}