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

Description(CHN)

有n种科目,每个科目有等级0~a[i]。开始时,每个科目都是0级。现在要选择一些课程进行学习使得每一个科目都达到最高等级。有m节课。对于每门课给出c1[i],L1[i],c2[i],L2[i],money[i],要选择这门课要求科目c1[i]的等级不小于L1[i],可以使科目c2[i]的等级升为L2[i],花费金钱money[i]。请计算最小花费是多少。

Solution

最小树形图

每门科目每个等级都设为一个点

所有课都对应一条权值为花费金钱的有向边

然后对于每一门课,将它的每个等级都向低一级连一条 \(0\) 费的边,那么只要到达高等级,低等级一定选到,并且不会影响最终代价

于是建个超级源点跑最小树形图就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=600+10,MAXM=MAXN*MAXN,inf=0x3f3f3f3f;
int n,m,sum[MAXN],vis[MAXN],pre[MAXN],in[MAXN],bel[MAXN],snt,s,a[MAXN];
struct node{
int u,v,k;
};
node side[MAXM];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline int id(int les,int lvl)
{
return sum[les-1]+lvl+1;
}
inline int solve(int rt,int n)
{
int res=0;
while(true)
{
for(register int i=1;i<=n;++i)in[i]=inf;
for(register int i=1;i<=snt;++i)
if(side[i].u!=side[i].v&&in[side[i].v]>side[i].k)in[side[i].v]=side[i].k,pre[side[i].v]=side[i].u;
for(register int i=1;i<=n;++i)
if(i!=rt&&in[i]==inf)return -1;
int cnt=0;
memset(vis,0,sizeof(vis));
memset(bel,0,sizeof(bel));
in[rt]=0;
for(register int i=1,j;i<=n;++i)
{
res+=in[i];j=i;
while(j!=rt&&vis[j]!=i&&!bel[j])vis[j]=i,j=pre[j];
if(j!=rt&&!bel[j])
{
bel[j]=++cnt;
for(register int k=pre[j];k!=j;k=pre[k])bel[k]=cnt;
}
}
if(!cnt)break;
for(register int i=1;i<=n;++i)
if(!bel[i])bel[i]=++cnt;
for(register int i=1,u,v;i<=snt;++i)
{
u=side[i].u,v=side[i].v;
side[i].u=bel[u],side[i].v=bel[v];
if(bel[u]^bel[v])side[i].k-=in[v];
}
n=cnt;
rt=bel[rt];
}
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)break;
snt=0;
for(register int i=1;i<=n;++i)read(a[i]),sum[i]=sum[i-1]+a[i]+1;
for(register int i=1;i<=m;++i)
{
int c1,l1,c2,l2,money;read(c1);read(l1);read(c2);read(l2);read(money);
side[++snt]=(node){id(c1,l1),id(c2,l2),money};
}
for(register int i=1;i<=n;++i)
for(register int j=a[i];j>=1;--j)side[++snt]=(node){id(i,j),id(i,j-1),0};
s=sum[n]+1;
for(register int i=1;i<=n;++i)side[++snt]=(node){s,id(i,0),0};
write(solve(s,s),'\n');
}
return 0;
}

【刷题】HDU 4966 GGS-DDU的更多相关文章

  1. HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)

    前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...

  2. 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)

    转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...

  3. 教你用python写:HDU刷题神器

    声明:本文以学习为目的,请不要影响他人正常判题 HDU刷题神器,早已被前辈们做出来了,不过没有见过用python写的.大一的时候见识了学长写这个,当时还是一脸懵逼,只知道这玩意儿好屌-.时隔一年,决定 ...

  4. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  5. NOIp2018停课刷题记录

    Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...

  6. LeetCode刷题系列

    LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...

  7. ife任务刷题总结(一)-css reset与清除浮动

    本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...

  8. 刷题ING...

    我用codeVS刷题.. 努力准备!!

  9. XidianOJ 1020 ACMer去刷题吧

    题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...

  10. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

随机推荐

  1. Session丢失——解决方案

    先抄下别人的作业(原帖:http://www.cnblogs.com/zhc088/archive/2011/07/24/2115497.html) Session丢失已经是一种习以为常的问题了,在自 ...

  2. Scrapy爬取美女图片第三集 代理ip(下)

    这是我的公众号获取原创保护的首篇文章,原创的肯定将支持我继续前行.现在写这篇文章的时间是晚上11:30,写完就回寝室休息了,希望更多的朋友与我一起同行(当然需要一个善良的妹子的救济).(我的新书< ...

  3. html学习第一天

    由于之后想做个网站,所以web前端的也要学习一下. 昨天看了一下html,今天做一下记录. 首先是安装工具,用文本编辑器有点麻烦,我选择的是强大的 Dreamweaver CS6,不过大家喜欢文本编辑 ...

  4. 搜索引擎ElasticSearch系列(五): ElasticSearch2.4.4 IK中文分词器插件安装

    一:IK分词器简介  IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本.最初,它是以开源 ...

  5. HDU-2844:Coins(多重背包+二进制优化)

    链接:HDU-2844:Coins 题意:给你n个种类的钱和对应的数量,同统计一下从1到m能够凑成的钱有多少个. 题解:C[i] = 1 + 2 + 4 + ··· + 2^k + a (0 < ...

  6. 最强NLP模型-BERT

    简介: BERT,全称Bidirectional Encoder Representations from Transformers,是一个预训练的语言模型,可以通过它得到文本表示,然后用于下游任务, ...

  7. tensorflow中使用mnist数据集训练全连接神经网络-学习笔记

    tensorflow中使用mnist数据集训练全连接神经网络 ——学习曹健老师“人工智能实践:tensorflow笔记”的学习笔记, 感谢曹老师 前期准备:mnist数据集下载,并存入data目录: ...

  8. 微软职位内部推荐-Principal Development Lead - SharePoint

    微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...

  9. 软银集团和共享办公空间公司WeWork在日本成立合资公司

    [TechWeb报道]7月18日消息,据国外媒体报道,软银集团和共享办公空间公司WeWork联合宣布,在日本成立合资公司WeWork Japan. 该合资公司将在日本开设联合办公空间,于明年初在东京设 ...

  10. Linux下使用vim编辑C程序

    这几天在系统能力班自学linux,加上最近大数据课上开始使用linux,我在这里总结一下,linux下使用vim编辑c程序的一些问题. 大数据课上是直接使用micro来编辑的,我这里只是简单的说明一下 ...