POJ 2516 基础费用流
阅读原文时间:2023年07月08日阅读:2

**题意

      有n个顾客,m个供应商,k种货物,给你顾客对于每种货物的要求个数,和供应商对于每种货物的现有量,以及供应每种货物的时候供应商和顾客之间的运输单价,问你满足所有顾客的前提下的最小运输费用是多少。

思路:
**

满足所有顾客的前提下的最小花费,很容易就想到了费用流,但是做这个题目有个小窍门,如果不想的话很可能把每个点拆成三个点,然后在去跑,但是这样感觉写着麻烦,AC肯定没问题,但是仔细想想,每种货物之间是没有任何关系的,那么我们何不直接每种货物都跑一遍费用流,这样写起来很简单,思路也清晰,建图的话应该不用说了吧,水建图。

#include<stdio.h>
#include<string.h>
#include<queue>

#define N 50 + 5
#define N_node 120
#define N_edge 6000
#define INF 100000000

using namespace std;

typedef struct
{
   int from ,to ,next ,cost ,flow;
}STAR;

STAR E[N_edge];
int list[N_node] ,tot;
int s_x[N_node] ,mer[N_edge];
int need[N][N] ,supply[N][N];

void add(int a ,int b ,int c ,int d)
{
   E[++tot].from = a;
   E[tot].to = b;
   E[tot].cost = c;
   E[tot].flow = d;
   E[tot].next = list[a];
   list[a] = tot;

   E[++tot].from = b;
   E[tot].to = a;
   E[tot].cost = -c;
   E[tot].flow = 0;
   E[tot].next = list[b];
   list[b] = tot;
}

bool spfa(int s ,int t ,int n)
{
   for(int i = 0 ;i <= n ;i ++)
   s_x[i] = INF;
   int mark[N_node] = {0};
   s_x[s] = 0 ,mark[s] = 1;
   queue<int>q;
   q.push(s);
   memset(mer ,255 ,sizeof(mer));
   while(!q.empty())
   {
      int xin ,tou = q.front();
      q.pop();
      mark[tou] = 0;
      for(int k = list[tou] ;k ;k = E[k].next)
      {
         xin = E[k].to;
         if(s_x[xin] > s_x[tou] + E[k].cost && E[k].flow)
         {
            s_x[xin] = s_x[tou] + E[k].cost;
            mer[xin] = k;
            if(!mark[xin])
            {
               mark[xin] = 1;
               q.push(xin);
            }
         }
      }
   }
   return mer[t] != -1;
}

int M_C_Flow(int s ,int t ,int n ,int sum)
{
   int maxflow = 0 ,mincost = 0 ,minflow;
   while(spfa(s ,t ,n))
   {
      minflow = INF;
      for(int i = mer[t] ;i + 1 ;i = mer[E[i].from])
      if(minflow > E[i].flow) minflow = E[i].flow;
      for(int i = mer[t] ;i + 1 ;i = mer[E[i].from])
      {
         E[i].flow -= minflow;
         E[i^1].flow += minflow;
         mincost += minflow * E[i].cost;
      }
      maxflow += minflow;
   }
   if(maxflow != sum) return -1;
   return mincost;
}

int main ()
{
   int n ,m ,k ,i ,j ,price;
   while(~scanf("%d %d %d" ,&n ,&m ,&k) && n + m + k)
   {
      for(i = 1 ;i <= n ;i ++)
      for(j = 1 ;j <= k ;j ++)
      scanf("%d" ,&need[i][j]);
      for(i = 1 ;i <= m ;i ++)
      for(j = 1 ;j <= k ;j ++)
      scanf("%d" ,&supply[i][j]);
      int ans = 0 ,mk = 0;
      for(int ii = 1 ;ii <= k ;ii ++)
      {
         memset(list ,0 ,sizeof(list)) ,tot = 1;
         for(i = 1 ;i <= n ;i ++)
         for(j = 1 ;j <= m ;j ++)
         {
            scanf("%d" ,&price);
            if(!price) continue;
            add(i ,j + n ,price ,INF);
         }
         if(mk) continue;
         int sum = 0;
         for(i = 1 ;i <= n ;i ++)
         {
            add(0 ,i ,0 ,need[i][ii]);
            sum += need[i][ii];
         }
         for(i = 1 ;i <= m ;i ++)
         add(i + n ,n + m + 1 ,0 ,supply[i][ii]);
         int tmp = M_C_Flow(0 ,n + m + 1 ,n + m + 1 ,sum);
         if(tmp == -1) mk = 1;
         else ans += tmp;
      }
      mk ? puts("-1") : printf("%d\n" ,ans);
   }
   return 0;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章