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

转载于:https://www.cnblogs.com/hongyj/p/9285748.html

【刷题】HDU 4966 GGS-DDU相关推荐

  1. python刷题软件_教你用python写:HDU刷题神器

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

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

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

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

    少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯瞰芸芸众生,唔....要做到这件事情可是需要一定天赋的哦! 博主本身也搞过一段时间的acm,对刷题深有感触,不信可以去看我博 ...

  4. 刷了几千道算法题,我私藏的刷题网站都在这里了

    作者 | Rocky0429 来源 | Python空间(ID: Devtogether) 遥想当年,机缘巧合入了 ACM 的坑,周边巨擘林立,从此过上了"天天被虐似死狗"的生活. ...

  5. 国内C/C++刷题网站汇总

    作者:Luau Lawrence 链接:https://www.zhihu.com/question/25574458/answer/31175374 来源:知乎 - Welcome To PKU J ...

  6. 光刷题不参加这些算法竞赛?太亏了!

    前言 大家好,我是bigsai.这篇文章给大家介绍算法竞赛,如果有帮助还请一键三连支持一下! 最近有些学妹问我咱们计算机专业的有哪些比赛可以参加呢?我眉头一皱,想了想咱们计算机专业竞赛好像确实蛮多的, ...

  7. 正确的LeetCode刷题姿势!

    名师 带你刷爆LeetCode 算法知识 讲解+训练 免费0元报名参加 在讲到 AI 算法工程师时,大部分同学关注点都在高大上的模型,一线优秀的项目.但大家往往忽略了一点,人工智能的模型.项目最终还是 ...

  8. python oj刷题网站_程序员常用的刷题网站

    1.Lintcode Lintcode.com--LintCode网站是国内较大的在线编程&测评网站.此网站提供各大IT公司的算法面试题类型,行分门别类,由简单到中等,再到难,便于不同水平的程 ...

  9. NOIP刷题网站系统noipoj

    刷题链接 http://noipoj.cn  NOIP OJ 在线评测系统 http://poj.org/ poj http://www.spoj.com/  spoj http://acm.hdu. ...

  10. 程序员常用刷题网站分享

    1. 牛客网 https://www.nowcoder.com/link/pc_csdncpt_qrsjd_c 该网站内集成了面试.题库.社群交流.课程教育.面试.招聘内推等多个模块.另外还是一个交流 ...

最新文章

  1. php socket ubuntu,Ubuntu升级php7.0配置fpm socket
  2. Eclipse 安装配置总结(WST WTP)(转)
  3. 玩玩机器学习3——TensorFlow基础之Session基本运算、占位符和变量的使用
  4. php 封装JavaScript类
  5. testflight怎么做版本更新_《动物森友会》万圣节版本更新后,别忘了做这五件事情...
  6. tensorflow2.0 图像处理项目_UCOSIII移植——STM32F769I 图像处理能力评测之五
  7. PyTorch入门(一)数据集的一些基础操作
  8. 高德地图坐标拾取组件
  9. xp计算机找不到音量调节,winxp系统电脑音量无法调节不能调节声音的恢复方案...
  10. div 空隙_尖叫到建筑的空隙
  11. Flask 和 requests 搭建一个简单的API服务
  12. 计算机开机配置失败6,win7开机配置windows update失败怎么跳过?-win7配置update失败,安全模式还是配置失败...
  13. Share:win10的日语输入法切换快捷键
  14. 算法题:岛屿最大面积
  15. 客户数据平台(CDP)是什么?
  16. 今天很悲剧,心情很郁闷
  17. swing hacks_这些firefox hacks和扩展程序增强了您的隐私
  18. mac挂载ntfs格式移动硬盘
  19. 操作系统 第七章 死锁
  20. 多线程开发Kafka消费者的方案和优劣

热门文章

  1. 第5课 开心的金明《聪明人的游戏:信息学探秘.提高篇》
  2. 基于java SSM框架的旅游网站设计开发(含源文件)
  3. Linux笔记-iptables模拟公司环境配置
  4. Qt文档阅读笔记-Qt单元测试中模拟GUI事件
  5. Linux工作笔记-配置.bashrc或.cshrc使core文件产生(方便gdb调试)
  6. 系统架构师学习笔记-操作系统(二)
  7. Java工作笔记-使用jquery.timer.js实现数据自动刷新
  8. Qt creator5.7 OpenCV249之图片膨化(含源码下载)
  9. AF_INET域与AF_UNIX域socket通信原理对比
  10. python 线性回归 统计检验 p值_PAST:最简便易用的统计学分析软件教程(一)软件基本信息介绍...