hdu 4966 GGS-DDU (最小树形图)

来源:互联网 发布:mac豆沙色口红推荐 编辑:程序博客网 时间:2024/05/29 02:21

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 43 3 11 0 2 3 102 1 1 2 101 2 3 1 103 1 1 3 100 0

Sample Output

40
题意:有n门课,每门也有一个最高等级ai,一开始每门等级的等级0。接着有m个研究班,对于每个研究班有a l1 b l2 w,表示只有第a门课程在l1及以上时才能上,花费代价w,可以将第b门课程提升到l2等级。现在就将所有的课程提升到最高等级需要付出的最小代价,若无法达到则输出-1。
分析:最小树形图,每门课程的每一个等级都看为一个点,然后对于同一课程,等级i向等级i-1建有向边,边权为0,其他的按照研究班的连有向边就行了
#include<cstdio>#include<cstring>#include<math.h>#include<algorithm>#define inf 1e9#define maxn 600#define maxm 200010using namespace std;struct node{    int st,en,len;    node(){}    node(int st,int en,int len): st(st),en(en),len(len){}}E[maxm];int n,m;int minin[maxn];int fa[maxn],id[maxn],pre[maxn];int zhuliu(int st){    int t,numn;    int sum=0;    while(1)    {        ///第一步,找每个点的最小入边及所对应的前驱节点        for(int i=0;i<=n;i++) minin[i]=inf;        for(int i=0;i<m;i++)        {            int u=E[i].st,v=E[i].en;            if(u!=v&&minin[v]>E[i].len)            {                minin[v]=E[i].len;                pre[v]=u;            }        }        minin[st]=0;        for(int i=0;i<=n;i++)            if(minin[i]>=inf) return -1;        ///第二步,查找和处理有向环        numn=-1;        minin[st]=0;        memset(id,-1,sizeof(id));        memset(fa,-1,sizeof(fa));        for(int i=0;i<=n;i++)        {            sum+=minin[i];            for(t=i;t!=st&&fa[t]!=i&&id[t]==-1;t=pre[t]) fa[t]=i;            if(t!=st&&id[t]==-1)            {                id[t]=++numn;                for(int j=pre[t];j!=t;j=pre[j]) id[j]=numn;            }        }        if(numn==-1) break;        //第三步,处理以及合并顶点        for(int i=0;i<=n;i++)            if(id[i]==-1) id[i]=++numn;        int numm=0;        for(int i=0;i<m;i++)        {            t=E[i].en;            E[i].st=id[E[i].st];            E[i].en=id[E[i].en];            if(E[i].st!=E[i].en)            {                E[numm]=node(E[i].st,E[i].en,E[i].len-minin[t]);                numm++;            }        }        n=numn,m=numm,st=id[st];    }    return sum;}int sum[110],a[110];int main(){    while(scanf("%d%d",&n,&m),n||m)    {        int num=0,st;        sum[0]=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            for(int j=a[i];j>0;j--)            {                E[num]=node(sum[i-1]+j,sum[i-1]+j-1,0);                num++;            }            sum[i]=sum[i-1]+a[i]+1;        }        st=sum[n];        for(int i=0;i<m;i++)        {            int c,l1,d,l2,w;            scanf("%d%d%d%d%d",&c,&l1,&d,&l2,&w);            E[num]=node(sum[c-1]+l1,sum[d-1]+l2,w);            num++;        }        for(int i=1;i<=n;i++)        {            E[num]=node(st,sum[i]-a[i]-1,0);            num++;        }        n=sum[n];        m=num;        printf("%d\n",zhuliu(st));    }    return 0;}

0 0
原创粉丝点击