hdu 4966 GGS-DDU(无固定根的最小树形图)
来源:互联网 发布:在windows上启动mysql 编辑:程序博客网 时间:2024/06/08 23:47
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!
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.
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门课程 每门课程都有一个最高等级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;}
0 0
- HDU 4966 GGS-DDU(无固定根最小树形图)
- hdu 4966 GGS-DDU(无固定根的最小树形图)
- HDU 4966 GGS-DDU 最小树形图
- HDU 4966 GGS-DDU 最小树形图
- 【HDU】4966 GGS-DDU 最小树形图
- HDU 4966 GGS-DDU 最小树形图
- HDU - 4966 GGS-DDU (最小树形图)
- HDU 4966 - GGS-DDU (最小树形图)
- hdu 4966 GGS-DDU (最小树形图)
- HDU 4966 GGS-DDU (最小树形图-朱刘算法)
- [HDOJ 4966] GGS-DDU [最小树形图]
- hdu 4966 GGS-DDU 最小树形图/有向图的最小生成树
- HDU-4966 GGS-DDU (最小树形图(有向图的最小生成树)[朱刘算法])
- HDU 4966(GGS-DDU-最小树形图)[Template:最小树形图]
- hdu4966 GGS-DDU --- 最小树形图
- HDU4966 GGS-DDU【最小树形图】
- HDU 4966 GGS-DDU
- HDU 2121 无固定根的最小树形图
- C++内存管理
- CMarkup的保存与读取
- WIN32串口编程详解(一)
- Codeforces544C:Writing Code(完全背包)
- druid项目实例
- hdu 4966 GGS-DDU(无固定根的最小树形图)
- 存储 key/value的hashtable
- Exe4j破解_Exe4j注册码
- WIN32串口编程详解(二)
- XDOJ 数独
- Cstring转char、string、int等数据类型的方法
- 监听密码框大小写提示
- Android:圆角ImageView
- Excel数据导入到oracle数据库