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;
}

最新文章

  1. 实现两个MySQL数据库之间的主从同步
  2. 布局display
  3. f2fs中node page的lock_page
  4. php常量的声明和使用
  5. OA项目之导出
  6. Javascript 笔记与总结(1-5)闭包
  7. XML序列化成对象
  8. exynos 4412 eMMC配置及使用方法
  9. Dictionary&lt;Key,Value&gt;的用法
  10. [ES6] Array -- Destructuring and Rest Parameters &amp;&amp; for ..of &amp;&amp; Arrat.find()
  11. 开源 免费 java CMS - FreeCMS1.9 会员组管理
  12. Xcode插件失效以后的处理方法
  13. 遍历Map集合:java.util.Map.Entry、KeySet两种方式
  14. Difference Between Git and SVN
  15. Java语言中姐种遍历List的方法总结
  16. Java SE之初探反射机制
  17. python解释器介绍以及Pycharm的破解
  18. kafka0.10
  19. magento 如何制作模板
  20. debian更新源时找不到公钥的解决办法

热门文章

  1. QSignalMapper类处理多信号关联同一个槽的方法(1)
  2. Qt 自定义事件的实现
  3. t-SNE可视化(MNIST例子)
  4. native生成策略:由Hibernate根据所使用的数据库支持能力从identity、sequence或者等生成策略中选择一种
  5. HGNC 数据库-人类基因组数据库
  6. MetaSploit Pro 下载地址
  7. 弹窗插件zDialog使用教程
  8. [转]SQL注入漏洞及绑定变量浅谈
  9. Ubuntu 使用文件 casper-rw 镜像文件 保存变更内容
  10. OpenCV学习:OpenCV介绍