Codevs 1028 Flower Shop window layout

Source: Internet
Author: User

Maximum cost maximum flow. Divided into two sets, the flow is 1, the cost is the given value.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#define MAXV 202
#define Maxe 20005
#define INF 12345678
using namespace Std;
int FF,VV,G[MAXV],PREV[MAXV],PREE[MAXV],DIS[MAXV];
BOOL USE[MAXV];
struct Edge
{
int v,f,c,nxt;
}e[maxe];
int s,t,x,nume=1;
void Addedge (int u,int v,int f,int c)
{
E[++nume].v=v;
E[nume].c=c;
E[nume].f=f;
E[nume].nxt=g[u];
G[u]=nume;
E[++nume].v=u;
E[nume].c=-c;
e[nume].f=0;
E[NUME].NXT=G[V];
G[v]=nume;
}
BOOL SPFA ()
{
Queue <int> q;
memset (dis,0x3f,sizeof (dis));
memset (pree,0,sizeof (Pree));
memset (prev,0,sizeof (prev));
dis[s]=0;
Q.push (s);
while (!q.empty ())
{
int Head=q.front ();
Q.pop ();
Use[head]=false;
for (int i=g[head];i;i=e[i].nxt)
{
int v=e[i].v;
if ((e[i].f>0) && (DIS[V]&GT;DIS[HEAD]+E[I].C))
{
DIS[V]=DIS[HEAD]+E[I].C;
Pree[v]=i;
Prev[v]=head;
if (!use[v])
{
Q.push (v);
Use[v]=true;
}

}
}
}
if (dis[t]==1061109567) return false;
return true;
}
int Dinic ()
{
int rq=0,u=t,deltas=inf;
while (U!=s)
{
Deltas=min (DELTAS,E[PREE[U]].F);
U=prev[u];
}
u=t;
while (U!=s)
{
int R=pree[u];
E[r].f=e[r].f-deltas;
E[r^1].f=e[r^1].f+deltas;
U=prev[u];
}
return dis[t]*deltas;
}
int main ()
{
s=0;
memset (G,0,sizeof (g));
scanf ("%d%d", &FF,&VV);
t=ff+vv+1;
for (int i=1;i<=ff;i++)
Addedge (s,i,1,0);
for (int i=1;i<=vv;i++)
Addedge (i+ff,t,1,0);
for (int i=1;i<=ff;i++)
for (int j=1;j<=vv;j++)
{
scanf ("%d", &x);
Addedge (i,j+ff,1,-x);
}
int maxcost=0;
while (SPFA () ==true)
Maxcost=maxcost+dinic ();
printf ("%d\n",-maxcost);
return 0;
}

Codevs 1028 Flower Shop window layout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.