日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【刷题】HDU 4966 GGS-DDU

發(fā)布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【刷题】HDU 4966 GGS-DDU 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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級。現(xiàn)在要選擇一些課程進(jìn)行學(xué)習(xí)使得每一個科目都達(dá)到最高等級。有m節(jié)課。對于每門課給出c1[i],L1[i],c2[i],L2[i],money[i],要選擇這門課要求科目c1[i]的等級不小于L1[i],可以使科目c2[i]的等級升為L2[i],花費金錢money[i]。請計算最小花費是多少。

Solution

最小樹形圖
每門科目每個等級都設(shè)為一個點
所有課都對應(yīng)一條權(quán)值為花費金錢的有向邊
然后對于每一門課,將它的每個等級都向低一級連一條 \(0\) 費的邊,那么只要到達(dá)高等級,低等級一定選到,并且不會影響最終代價
于是建個超級源點跑最小樹形圖就好了

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

轉(zhuǎn)載于:https://www.cnblogs.com/hongyj/p/9285748.html

總結(jié)

以上是生活随笔為你收集整理的【刷题】HDU 4966 GGS-DDU的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。