NEU 1458 square fetch (cost stream of network Stream)

Source: Internet
Author: User

Address: neu 1458

The two squares on hangdian are not the same .. This can be repeated, but the sum can only be added once. The idea of creating a graph is coming soon. The same split point only requires another split edge. One of them costs 0, and the other is the value at that point. The traffic is limited to 1. Then the remaining two are similar to those on Hangzhou power. Because the array is opened several times lower than wa .. (I checked the array size before, but I thought it was not small yet... You are speechless ..)

The Code is as follows:

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int head[21000], source, sink, cnt, mp[101][101], flow, cost, n;int vis[21000], d[21000], cur[21000];struct node{    int u, v, cap, cost, next;} edge[1000000];void add(int u, int v, int cap, int cost){    edge[cnt].v=v;    edge[cnt].cap=cap;    edge[cnt].cost=cost;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].cap=0;    edge[cnt].cost=-cost;    edge[cnt].next=head[v];    head[v]=cnt++;}int spfa(){    int i, minflow=INF;    memset(d,INF,sizeof(d));    memset(vis,0,sizeof(vis));    cur[source]=-1;    d[source]=0;    queue<int>q;    q.push(source);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if(d[v]>d[u]+edge[i].cost&&edge[i].cap)            {                d[v]=d[u]+edge[i].cost;                minflow=min(minflow,edge[i].cap);                cur[v]=i;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    if(d[sink]==INF) return 0;    flow+=minflow;    cost-=minflow*d[sink];    //printf("%d %d\n",minflow,d[sink]);    for(i=cur[sink]; i!=-1; i=cur[edge[i^1].v])    {        edge[i].cap-=minflow;        edge[i^1].cap+=minflow;    }    return 1;}void mcmf(){    flow=cost=0;    while(spfa()) ;    printf("%d\n",cost);}int main(){    int i, j;    while(scanf("%d",&n)!=EOF)    {        for(i=1; i<=n; i++)        {            for(j=1; j<=n; j++)            {                scanf("%d",&mp[i][j]);            }        }        memset(head,-1,sizeof(head));        cnt=0;        source=0;        sink=2*n*n+1;        add(source,1,2,0);        add(2*n*n,sink,2,0);        for(i=1; i<=n; i++)        {            for(j=1; j<=n; j++)            {                add((i-1)*n+j,(i-1)*n+j+n*n,1,0);                add((i-1)*n+j,(i-1)*n+j+n*n,1,-mp[i][j]);                if(1+j<=n)                {                    add((i-1)*n+j+n*n,(i-1)*n+j+1,2,0);                }                if(i+1<=n)                {                    add((i-1)*n+j+n*n,i*n+j,2,0);                }            }        }        mcmf();    }    return 0;}


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.