2014多校9(1007)hdu4966(最小树形图)

GGS-DDU

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


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个科目,以及每门科目需要达到的等级,然后给出了m堂课,以及修每堂课要求达到的科目等级,修完以后可以获得的科目等级以及修这门课的花费,然后求每        
        门科目都到达满级的最小花费,如果不能完成则输出-1

思路:将每门科目的每个等级看成一个点,然后对于同一门科目,等级 i 向等级 i-1 连一条边,花费为0,然后给出的m堂课程,从要求的科目等级 i 向可以获得的科目等级 j 连

        边,花费为给出的花费,将所有课程的等级0缩成一个根结点,然后问题就转化为求最小树形图,也叫有向图上的最小生成树,直接套板子搞定~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;
#define INF ((1<<31)-1)

int n,m;
struct node{
    int u;
    int v;
    int w;
};
int head[550];
int next[2550];
node edge[2550];
int vis[550];
int a[55];
int fa[550];
int in[550];
int id[550];
int d;

void add(int u,int v,int w)
{
    edge[d].u=u;
    edge[d].v=v;
    edge[d].w=w;
    next[d]=head[u];
    head[u]=d++;
}

void dfs(int u)
{
    if(vis[u])return;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=next[i])dfs(edge[i].v);
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(!n&&!m)break;
        int i,j,x,u,v,u1,v1;
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        d=0;
        int total=0;
        int ans=0;

        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(!a[i])continue;
            add(total+1,0,0);
            for(j=total+2;j<=total+a[i];j++)add(j,j-1,0);
            total+=a[i];
        }

        for(i=1;i<=n;i++)a[i]+=a[i-1];

        for(i=0;i
       
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值