hdu 4966 GGS-DDU(无固定根的最小树形图)

GGS-DDU

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 561    Accepted Submission(s): 269


Problem Description
Do you think this is a strange problem name? That is because you don't know its full name---'Good Good Study and Day Day Up!". Very famous sentence! Isn't it?

Now "GGS-DDU" is lzqxh's target! He has N courses and every course is divided into a plurality of levels. Just like College English have Level 4 and Level 6.

To simplify the problem, we suppose that the i-th course has Levels from level 0 to level a[i]. And at the beginning, lzqxh is at Level 0 of every course. Because his target is "GGS-DDU", lzqxh wants to reach the highest Level of every course. 

Fortunately, there are M tutorial classes. The i-th tutoial class requires that students must reach at least Level L1[i] of course c[i] before class begins. And after finishing the i-th tutorial class, the students will reach Level L2[i] of course d[i]. The i-th tutoial class costs lzqxh money[i]. 

For example, there is a tutorial class only students who reach at least Level 5 of "Tiyu" can apply. And after finishing this class, the student's "MeiShu" will reach Level 10 if his "MeiShu"'s Level is lower than 10. (Don't ask me why! Supernatural class!!!")

Now you task is to help lzqxh to compute the minimum cost!
 

Input
The input contains multiple test cases.

The first line of each case consists of two integers, N (N<=50) and M (M<=2000). 
The following line contains N integers, representing a[1] to a[N]. The sum of a[1] to a[N] will not exceed 500. 
The next M lines, each have five integers, indicating c[i], L1[i], d[i], L2[i] and money[i] (1<=c[i], d[i]<=N, 0<=L1[i]<=a[c[i]], 0<=L2[i]<=a[d[i]], money[i]<=1000) for the i-th tutorial class. The courses are numbered from 1 to N.

The input is terminated by N = M = 0.
 

Output
Output the minimum cost for achieving lzqxh's target in a line. If his target can't be achieved, just output -1.
 

Sample Input
  
  
3 4 3 3 1 1 0 2 3 10 2 1 1 2 10 1 2 3 1 10 3 1 1 3 10 0 0
 

Sample Output
  
  
40
 


n门课程 每门课程都有一个最高等级a[i] 现在要求最少花多少钱可以达到所有课程的最高等级

把每门课程的每个等级都看作一个节点 每门课程的第i等级与第i-1等级之间建边 权值为0

虚拟根与每个课程之间建边权值为0 

从课程c的l1等级到课程d的l2等级 权值为所需要的费用

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 10010
#define INF 1000000000
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct Edge
{
    int u,v;
    int cost;
}edge[MAXN*MAXN];
int pre[MAXN],id[MAXN],vis[MAXN];
int in[MAXN];
int ansid;
int zhuliu(int root,int n,int m)
{
    int res=0;
    int u,v;
    while(1)
    {
        for(int i=0;i<n;i++)
            in[i]=INF;
        for(int i=0;i<m;i++)
            if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v])
        {
            pre[edge[i].v]=edge[i].u;
            in[edge[i].v]=edge[i].cost;
            if(edge[i].u==root) ansid=i;
        }
        for(int i=0;i<n;i++)
            if(i!=root&&in[i]==INF)
                return -1;
        int tn=0;
        MEM(id,-1);
        MEM(vis,-1);
        in[root]=0;
        for(int i=0;i<n;i++)
        {
            res+=in[i];
            v=i;
            while(vis[v]!=i&&id[v]==-1&&v!=root)
            {
                vis[v]=i;
                v=pre[v];
            }
            if(v!=root&&id[v]==-1)
            {
                for(int u=pre[v];u!=v;u=pre[u])
                    id[u]=tn;
                id[v]=tn++;
            }
        }
        if(tn==0) break;
        for(int i=0;i<n;i++)
            if(id[i]==-1)
                id[i]=tn++;
        for(int i=0;i<m;i++)
        {
//            int uu = edge[i].u;
//            int vv = edge[i].v;
//            edge[i].u = id[uu];
//            edge[i].v = id[vv];
//            if(id[uu] != id[vv])
//                edge[i].cost -= in[vv];
            v=edge[i].v;
            edge[i].u=id[edge[i].u];
            edge[i].v=id[edge[i].v];
            if(edge[i].u!=edge[i].v)
                edge[i].cost-=in[v];
        }
        n=tn;
        root=id[root];
    }
    return res;
}
int g[MAXN][MAXN];
int a[MAXN];
int sum[MAXN];

int main()
{
//    fread;
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i]+1;
        }
        int tot=sum[n]+1;
        int start=sum[n];
        for(int i=0;i<tot;i++)
            for(int j=0;j<tot;j++)
                g[i][j]=INF;
        for(int i=0;i<n;i++)
            g[start][sum[i]]=0;
        int c,d,l1,l2,mon;
        while(m--)
        {
            scanf("%d%d%d%d%d",&c,&l1,&d,&l2,&mon);
            int u=sum[c-1]+l1;
            int v=sum[d-1]+l2;
            g[u][v]=min(g[u][v],mon);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=a[i];j++)
                g[sum[i-1]+j][sum[i-1]+j-1]=0;
        int num=0;
        for(int i=0;i<tot;i++)
            for(int j=0;j<tot;j++)
                if(g[i][j]<INF)
        {
            edge[num].u=i; edge[num].v=j;
            edge[num++].cost=g[i][j];
        }
        int ans=zhuliu(start,tot,num);
        printf("%d\n",ans);
    }
    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值