GGS-DDU

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 318    Accepted Submission(s): 166
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门课程,每个课程有0到a[i]的等级划分;现在有m个选修课,第i个选修课需要第c[i]的课程至少达到L1[i]的等级,修完后能让第d[i]个课程达到
L2[i]的等级,问至少花费多少可以让每个课程都达到最高水平,如果不能实现目标输出-1;
官方题解:
把每门课的每个等级看作是一个节点,对于没门课程,第j个等级向第j-1个等级连一条边,费用为0,对于第i个选修课,如果每个class需要class11的等级至少L1,修完后可使class12的等级到达L2,则把class11到class12连一条边费用是money[i];每门课的0等级合并作为根节点,这样就转化为以根节点出发到达其他节点的最小费用,此时就是最小树形图了;
#include"string.h"
#include"stdio.h"
#include"math.h"
#include"queue"
#define eps 1e-10
#define M 26000
#define inf 100000000
using namespace std;
struct node
{
int x,y,z;
}p[M];
struct Edge
{
int u,v;
int w;
}edge[30000];
int pre[M],id[M],use[M],in[M],a[M],s[M];
int Fabs(int x)
{
return x>0?x:-x;
}
void add(int u,int v,int w,int m)
{
edge[m].u=u;
edge[m].v=v;
edge[m].w=w;
}
int mini_tree(int root,int n,int m)//分别是树根,节点数,边数,序号从1开始
{
int ans=0;
int i,u;
while(1)
{
for(i=1;i<=n;i++)
in[i]=inf;
for(i=1;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(edge[i].w<in[v]&&u!=v)
{
in[v]=edge[i].w;
pre[v]=u;
}
}//找最小的入边
for(i=1;i<=n;i++)
{
if(i==root)continue;
ans+=in[i];//把边权加起来
if(in[i]==inf)//如果存在没有入弧的点则不存在最小树形图
return -1;
}
memset(id,-1,sizeof(id));
memset(use,-1,sizeof(use));
int cnt=0;
for(i=1;i<=n;i++)//枚举每个点,搜索找环
{
int v=i;
while(v!=root&&use[v]!=i&&id[v]==-1)
{
use[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-1)//当找到环的时候缩点编号
{
++cnt;
id[v]=cnt;
for(u=pre[v];u!=v;u=pre[u])
id[u]=cnt;
}
}
if(cnt==0)//如果没有环结束程序
break;
for(i=1;i<=n;i++)//把余下的不在环里的点编号
if(id[i]==-1)
id[i]=++cnt;
for(i=1;i<=m;i++)//建立新的图
{
int u=edge[i].u;
int v=edge[i].v;
edge[i].u=id[u];
edge[i].v=id[v];
if(edge[i].u!=edge[i].v)
edge[i].w-=in[v];
}
n=cnt;//更新节点数和根节点的编号
root=id[root];
}
return ans;
}
int main()
{
int n,m,i,j,c,d,L1,L2,money;
while(scanf("%d%d",&n,&m),m||n)
{
s[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[i]=s[i-1]+a[i];
}
int E=0;
int root=s[n]+1;
int V=s[n]+1;
for(i=1;i<=n;i++)
{
for(j=s[i-1]+2;j<=s[i];j++)
add(j,j-1,0,++E);
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d%d",&c,&L1,&d,&L2,&money);
int u,v;
if(L1==0)
u=root;
else
u=s[c-1]+L1;
if(L2==0)
v=root;
else
v=s[d-1]+L2;
if(u!=v)
add(u,v,money,++E);
}
int ans=mini_tree(root,V,E);
printf("%d\n",ans);
}
return 0;
}

最小树形图(hdu4966多校联赛9)的更多相关文章

  1. HDU4966 GGS-DDU(最小树形图)

    之前几天想着补些算法的知识,学了一下最小树形图的朱刘算法,不是特别理解,备了份模板以备不时之需,想不到多校冷不丁的出了个最小树形图,没看出来只能表示对算法不太理解吧,用模板写了一下,然后就过了.- - ...

  2. hdu4966 最小树形图+虚根

    /* 辛辛苦苦调试半天, 过了样例,竟然没有ac!! 网上对比了ac代码,感觉添加一个虚根就能ac 但是想不明白为什么 */ /* 第二天想了下,知道了为什么wa:因为从等级0连到其他课程等级i的不止 ...

  3. hdu4966 最小树形图(最少辅导花费)

    题意:       以一些科目,和辅导班,每个科目最终要求修到某个等级,可以花一定的钱在辅导班把某一科目修到某一等级,进入辅导班的时候会有一个限制,那就是达到他给出的科目和等级限制,比如a b c d ...

  4. bzoj4349: 最小树形图

    最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 ...

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

    比较好的讲解:http://blog.csdn.net/wsniyufang/article/details/6747392 view code//首先为除根之外的每个点选定一条入边,这条入边一定要是 ...

  6. HDU 4966 GGS-DDU(最小树形图)

    n个技能,每个技能有0-a[i]的等级,m个课程,每个课程需要前置技能c[i]至少达到lv1[i]等级,效果是技能d[i]达到lv2[i]等级,花费w[i]. 输出最小花费使得全技能满级(初始全技能0 ...

  7. hdu3072 强连通+最小树形图

    题意:有一个人他要把一个消息通知到所有人,已知一些通知关系:A 能通知 B,需要花费 v,而又知道,如果某一个小团体,其中的成员相互都能直接或间接通知到,那么他们之间的消息传递是不需要花费的,现在问这 ...

  8. POJ3164 Command Network(最小树形图)

    图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...

  9. HDU ACM 2121 Ice_cream’s world II (无根最小树形图)

    [解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...

随机推荐

  1. tensorflow函数解析:Session.run和Tensor.eval的区别

    tensorflow函数解析:Session.run和Tensor.eval 翻译 2017年04月20日 15:05:50 标签: tensorflow / 机器学习 / 深度学习 / python ...

  2. Java多例模式

    多例模式又划分为有上限多例模式和无上限多例模式两种,没上限的多例模式和直接 new 一个对象没什么差别,此处不做记录. 有上限多例模式:实际上是单例模式的推广,如果它的上限是1,那么就成了单例模式了. ...

  3. 批量快速的导入导出Oracle的数据(spool缓冲池、java实现)

    1. Java代码实现思路 BufferedWriter writefile = new BufferedWriter(new FileWriter(file));  writefile.write( ...

  4. 讨论CSS中的各类居中方式

    今天主要谈一谈CSS中的各种居中的办法. 首先是水平居中,最简单的办法当然就是 margin:0 auto; 也就是将margin-left和margin-right属性设置为auto,从而达到水平居 ...

  5. Unity-Animator(Mecanim)深入系列总索引

    花了不少时间完成了这篇Unity Animator学习系列文章,其中大多数内容都来自个人实践,包括API部分很多都是亲测,期望和网上的诸多教程达到互补. 相关参考文档 Unity Animator官方 ...

  6. win7 安装 VMware 出错解决办法

    win7旗舰版安装VMware,安装过程中发生了如下错误.系统提示:“You may not install this product in the root directory of any dri ...

  7. 理解ros话题--6

    理解ROS话题(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) Description: 本教程介绍ROS话题(topics)以及如何使用ro ...

  8. js timestamp与datetime之间的相互转换

    1.  datetime转换成timestamp strdate = "2015-08-09 08:01:36:"; var d = new Date(strdate); var ...

  9. java图形化Swing教程(一)

    与多线程.泛型等不同,Swing主要在于使用. 以下主要放代码和凝视.少说话. (一)基本框架 package Swing; import java.awt.*; import javax.swing ...

  10. 实例教程Unity3D单例模式(二)自我包括法

    unity3d 里的单例模式自我包括法 有一次玩Trench Run game,我意识到我的场景类里存在很多的GameObject.所以,我开发了自我包括的单例.假设没找找到实例,就会创建它自己的Ga ...