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
 

Author
SYSU
 

题意:已知有n门课程,每个课程都有等级0-A[i],lzqxh想每门课程都修到最高分A[i],(起始都是0分)。有m种班级可以提分,花费是money[i],要求c课程的分数必须至少达到L1,课程结束后d课程的分数可以提到L2,问至少花多少钱

做法:因为是搜的分类,知道是最小生成树,很容易想到是把每个课程的每个等级拆成一个节点,最终求所有节点的最小生成树即可。但是熟知的最小生成树算法是针对无向图的,貌似有一个叫做“最小树形图”的算法哈,拿来试试~其实之前自己就想到了应该把所有点的等级从高到低连边,花费是0。但是所有点的0点不是一个啊!然后就是按着题意的条件连边就好啦。注意数组不要开小啦,因为这个WA了一晚上

    #include <cstdio>
    #include <iostream>
    #include<queue>
    #include<set>
    #include<ctime>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<cstring>
    using namespace std;
    const double eps=1e-10;
    #define M 25000
    #define type int
    const type inf=(1)<<30;
    struct Node{
        int u , v;
        type cost;
    }E[29005];
    int pre[M],ID[M],vis[M];
    type In[M];
    int n,m;
    type Directed_MST(int root,int NV,int NE) {
        type ret = 0;
        while(true) {
            //1.找最小入边
            for(int i=0;i<NV;i++) In[i] = inf;
            for(int i=0;i<NE;i++){
                int u = E[i].u;
                int v = E[i].v;
                if(E[i].cost < In[v] && u != v) {
                    pre[v] = u;
                    In[v] = E[i].cost;
                }
            }
            for(int i=0;i<NV;i++) {
                if(i == root) continue;
                if(In[i] == inf)    return -1;//除了跟以外有点没有入边,则根无法到达它
            }
            //2.找环
            int cntnode = 0;
            memset(ID,-1,sizeof(ID));
            memset(vis,-1,sizeof(vis));
            In[root] = 0;
            for(int i=0;i<NV;i++) {//标记每个环
                ret += In[i];
                int 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] = cntnode;
                    }
                    ID[v] = cntnode ++;
                }
            }
            if(cntnode == 0)    break;//无环
            for(int i=0;i<NV;i++) if(ID[i] == -1) {
                ID[i] = cntnode ++;
            }
            //3.缩点,重新标记
            for(int i=0;i<NE;i++) {
                int v = E[i].v;
                E[i].u = ID[E[i].u];
                E[i].v = ID[E[i].v];
                if(E[i].u != E[i].v) {
                    E[i].cost -= In[v];
                }
            }
            NV = cntnode;
            root = ID[root];
        }
        return ret;
    }

    int A[5000],sum[5000];
    int main()
    {
       // freopen("cin.txt","r",stdin);
        while(~scanf("%d%d",&n,&m))
        {
            if(n==0&&m==0)break;
            int count=0,cnt=0;
            sum[0]=0;
            for(int i=0;i<n;i++){
                scanf("%d",&A[i]);
                A[i]++;
                sum[i+1]=sum[i]+A[i];
            }
            for(int i=0;i<n;i++)
            {
                for(int j=sum[i+1]-1;j>sum[i];j--)
                {
                    E[cnt].u=j;
                    E[cnt].v=j-1;
                    E[cnt++].cost=0;
                }
                E[cnt].u=sum[n];
                E[cnt].v=sum[i];
                E[cnt++].cost=0;
            }
            while(m--)
            {
                //c[i], L1[i], d[i], L2[i] and money[i]
                int c,l1,d,l2,money;
                scanf("%d%d%d%d%d",&c,&l1,&d,&l2,&money);
                E[cnt].u=sum[c-1]+l1;
                E[cnt].v=sum[d-1]+l2;
                E[cnt++].cost=money;
            }
            type ans=Directed_MST(sum[n],sum[n]+1,cnt);
            printf("%d\n",ans);
        }
        return 0;
    }