HDU 4780 Candy Factory

ProblemDescription

  A newcandy factory opens in pku-town. The factory import M machines to produce highquality candies. These machines are numbered from 1 to M.
  There are N candies need to be produced. These candies are alsonumbered from 1 to N. For each candy i , it can be produced in any machine j.It also has a producing time(si,ti) , meaning that candyi must start producing at time si
 and will finish atti. Otherwise if the start time is pi(si < pi < ti)then candy will still finish at ti but needadditional K*(pi - si) cost. The candy can’t beproduced if pi is greater than or equal to ti.Of course one machine can only produce at most one candy at a time and can’tstop once start producing.
  On the other hand, at time 0 all the machines are in their initialstate and need to be “set up” or changed before starting producing. To set upMachine j from its initial state to the state which is suitable for producingcandiy i, the time required is Cij
 and cost is Dij.To change a machine from the state suitable for candy i1 into the statesuitable for candy i2, time required is Ei1i2 and cost is Fi1i2.
  As the manager of the factory you have to make a plan to produceall the N candies. While the sum of producing cost should be minimized.

 

 

Input

There are multiple test cases.
For each case, the first line contains threeintegers N(1<=N<=100), M(1<=M<=100), K(1<=K<=100) . Themeaning is described above.
Then N lines follow, each line contains 2integers si and ti(0 <= si < ti <100000).
Then N lines follow, each line contains Mintegers, the j-th integer of the i-th line indicating Cij(1<=Cij<=100000).
Then N lines follow, each line contains Mintegers, the j-th integer of the i-th line indicating Dij(1<=Dij<=100000).
Then N lines follow, each line contains Nintegers, the i2-th integer of the i1-th line indicatingEi1i2(1<=Ei1j2<=100000) . 
Then N lines follow, each line contains Nintegers, the i2-th integer of the i1-th line indicatingFi1i2(1 <= Fi1j2<=100000) . 
Since the same candy will only be producedonce, Eii and Fii are meaninglessand will always be -1.
The input ends by N=0 M=0 K=0. Cases areseparated with a blank line.

 

 

Output

  Foreach test case, if all of M candies can be produced, output the sum of minimumproducing cost in a single line. Otherwise output -1.

 

 

Sample Input

3 2 1

4 7

2 4

8 9

4 4

3 3

3 3

2 8

12 3

14 6

-1 1 1

1 -1 1

1 1 -1

-1 5 5

5 -1 5

5 5 -1

 

1 1 2

1 5

5

5

-1

-1

 

0 0 0

 

 

Sample Output

11

-1

 集训期间,实在太累,不写了。。。。。。待到开学再来慢慢写吧

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>>
#define INF 1000000000
using namespace std;
const int M = 10 * 10010;//边

struct Node//边,点f到点t,流量为c,费用为w
{
	int st, ed;
	int flow, cost;
	int next;
}edge[M];
int head[M], dis[M], q[M], pre[M], cnt;//cnt为已添加的边数,head为邻接表,dis为花费,pre为父亲节点
bool vis[M];

int c[110][110], d[110][110], e[110][110], f[110][110];
int n, m, k ,s ,t;
struct ME
{
    int start ,en;
}me[110];

void init()
{
	memset(head, -1, sizeof(head));
	cnt = 0;
}

void add_edge(int f, int t, int d1, int d2, int w)
{//f到t的一条边,流量为d1,反向流量d2,花费w,反向边花费-w(可以反悔)
	edge[cnt].st = f;
	edge[cnt].ed = t;
	edge[cnt].flow = d1;
	edge[cnt].cost = w;
	edge[cnt].next = head[f];
	head[f] = cnt++;

	edge[cnt].st = t;
	edge[cnt].ed = f;
	edge[cnt].flow = d2;
	edge[cnt].cost = -w;
	edge[cnt].next = head[t];
	head[t] = cnt++;
}

bool spfa(int s, int t, int n)
{
	int i, tmp, l, r;
	memset(pre, -1, sizeof(pre));
	for(i = 0; i < n; ++i)
    {
        dis[i] = INF;
    }
	dis[s] = 0;
	q[0] = s;
	l = 0, r = 1;
	vis[s] = true;
	while(l != r)
    {
		tmp = q[l];
		l = (l + 1) % (n + 1);
		vis[tmp] = false;
		for(i = head[tmp]; i!=-1; i = edge[i].next)
		{
			if(edge[i].flow && dis[edge[i].ed] > dis[tmp] + edge[i].cost)
			{
				dis[edge[i].ed] = dis[tmp] + edge[i].cost;
				pre[edge[i].ed] = i;
				if(!vis[edge[i].ed])
				{
					vis[edge[i].ed] = true;
					q[r] = edge[i].ed;
					r = (r + 1) % (n + 1);
				}
			}
		}
	}
	if(pre[t] == -1)
	{
	    return false;
	}
	return true;
}

void MCMF(int s, int t, int n, int &flow, int &cost)
{//起点s,终点t,点数n,最大流flow,最小花费cost
	int tmp, arg;
	flow = cost = 0;
	while(spfa(s, t, n))
    {
		arg = INF;
		tmp = t;
		while(tmp != s)
		{
			arg = min(arg, edge[pre[tmp]].flow);
			tmp = edge[pre[tmp]].st;
		}
		tmp = t;
		while(tmp != s)
		{
			edge[pre[tmp]].flow -= arg;
			edge[pre[tmp] ^ 1].flow += arg;
			tmp = edge[pre[tmp]].st;
		}
		flow += arg;
		cost += arg * dis[t];
	}
}

int main()
{
    int no ,no1 ,no2;
    int flow, cost;
    while(~scanf("%d%d%d",&n,&m,&k),n+m+k!=0)
    {
        for(int i = 1;i<=n;i++)
        {
            scanf("%d%d",&me[i].start,&me[i].en);
        }
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                scanf("%d",&c[i][j]);
            }
        }
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                scanf("%d",&d[i][j]);
            }
        }
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=n;j++)
            {
                scanf("%d",&e[i][j]);
            }
        }
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=n;j++)
            {
                scanf("%d",&f[i][j]);
            }
        }
        no = m;
		init();
        for(int i = 1;i<=m;i++)
        {
            add_edge(0,i,1,0,0);
			for(int j = 1;j<=n;j++)
			{
				if(c[j][i] <= me[j].start)
				{
					add_edge(i,no + j,1,0,d[j][i]);
				}
				else if(c[j][i] < me[j].en)
				{
					add_edge(i,no + j,1,0,k * (c[j][i] - me[j].start) + d[j][i]);
				}
			}
        }
        no1 = m + n;
        no2 = no1 + n;
        t = no2 + n + 1;
        for(int i = 1;i<=n;i++)
        {
            add_edge(0,no1 + i,1,0,0);
            add_edge(no + i,no2 + i,1,0,0);
            for(int j = 1;j<=n;j++)
            {
				if(i!=j)
				{
					if(me[i].en + e[i][j] <= me[j].start)
					{
						add_edge(no1 + i,no2 + j,1,0,f[i][j]);
					}
					else if(me[i].en + e[i][j] < me[j].en)
					{
						add_edge(no1 + i,no2 + j,1,0,k * ( me[i].en + e[i][j] - me[j].start) + f[i][j]);
					}
				}
            }
            add_edge(no2 + i,t,1,0,0);
        }
        MCMF(0,t,t+1,flow,cost);
        if(flow==n)
        {
            printf("%d\n",cost);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值