HDU 4966(GGS-DDU-最小树形图)[Template:最小树形图]

GGS-DDU

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


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
 

Source
 

Recommend
We have carefully selected several similar problems for you:   5431  5430  5429  5428  5427 
 


第一题最小树形图


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cctype>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define MAXN (500+50)
#define MAXM (2000+10) 
#define MAXS (500+60)
#define MAXE (10000+10)
typedef __int64 ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}


int n,m,a[MAXN],s[MAXN];
struct edge{
	int u,v,w;
	edge(){}
	edge(int _u,int _v,int _w):u(_u),v(_v),w(_w){}
}e[MAXE];
int tot=0;

int id[MAXN],in[MAXN],pre[MAXN],vis[MAXN];
int MST_Directed(int root,int numv,int nume) {
	int ans=0;
 
	while (1) {
		For(i,numv) in[i]=INF,pre[i]=INF;
		For(i,nume) {
			int u=e[i].u,v=e[i].v,w=e[i].w;
			if (w<in[v]&&u!=v) {
				pre[v]=u;
				in[v]=w;
			}
		}
		
		For(i,numv) {
			if (i==root) continue;
			if (in[i]==INF) return -1;
		} 
		in[root]=0;
		For(i,numv) ans+=in[i];
		
		memset(id,-1,sizeof(id));
		memset(vis,-1,sizeof(vis));
		int cntnode=0; 
	
		For(i,numv) {
			int v=i;
			while( vis[ v ] !=i && id[v] == -1 && (v!=root) ) {
				vis[v]=i;
				v=pre[v];
			}	
			if (v!=root && id[v] == -1 ) {
				++cntnode;
				for(int u=pre[v];u!=v;u=pre[u]) id[u]=cntnode;
				id[v]= cntnode; 
			}		
		}
		
		if (!cntnode) break;
		For(i,numv) {
			if (id[i]==-1) {
				id[i]= ++ cntnode; 
			}
		}
		
		For(i,nume) {
			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].w-=in[v];
			}
		}
				
		numv=cntnode;
		root=id[root];
		
	}
	return ans;
}


int main()
{
//	freopen("data.in","r",stdin);
//	freopen(".out","w",stdout);
	
	while(scanf("%d%d",&n,&m)==2&&n&&m) {
		For(i,n) scanf("%d",&a[i]),a[i]++;
		s[0]=0; For(i,n) s[i]=a[i]+s[i-1];
		
		tot=0;
		int root=s[n]+1;
		For(i,n) {
			Fork(j,s[i-1]+2,s[i]) e[++tot]=edge(j,j-1,0);
		}
		
		For(i,n) e[++tot]=edge(root,s[i-1]+1,0);
		
		
		For(i,m) {
			int c,l1,d,l2,money;
			scanf("%d%d%d%d%d",&c,&l1,&d,&l2,&money); l1++,l2++;
			
			e[++tot]=edge(s[c-1]+l1,s[d-1]+l2,money);
		}
		
		printf("%d\n",MST_Directed(root,s[n]+1,tot) );
		
	}
	
	return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值