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

HDU4966

最小树形图, 模板题。

GGS-DDU

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 302 Accepted Submission(s): 156

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

Author

SYSU

Source

2014 Multi-University Training Contest 9

直接建图。套用最小树形图模板。

/* ***
Author :kuangbin
Created Time :2014/8/19 13:01:24
File Name :20140819\1007.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1010;
const int MAXM = 400100;

struct Edge
{
int u,v,cost;
};
Edge edge[MAXM];
int pre[MAXN],id[MAXN],visit[MAXN],in[MAXN];
int gao(int root,int n,int m,Edge edge[])
{
int res = 0,u,v;
while(1)
{
for(int i = 0;i < n;i++)
in[i] = INF;
for(int i = 0;i < m;i++)
if(edge[i].u != edge[i].v && edge[i].cost < in[edge[i].v])
{
pre[edge[i].v] = edge[i].u;
in[edge[i].v] = edge[i].cost;
}
for(int i = 0;i < n;i++)
if(i != root && in[i] == INF)
return -1;
int tn = 0;
memset(id,-1,sizeof(id));
memset(visit,-1,sizeof(visit));
in[root] = 0;
for(int i = 0;i < n;i++)
{
res += in[i];
v = i;
while( visit[v] != i && id[v] == -1 && v != root)
{
visit[v] = i;
v = pre[v];
}
if( v != root && id[v] == -1 )
{
for(int u = pre[v]; u != v ;u = pre[u])
id[u] = tn;
id[v] = tn++;
}
}
if(tn == 0)break;
for(int i = 0;i < n;i++)
if(id[i] == -1)
id[i] = tn++;
for(int i = 0;i < m;)
{
v = edge[i].v;
edge[i].u = id[edge[i].u];
edge[i].v = id[edge[i].v];
if(edge[i].u != edge[i].v)
edge[i++].cost -= in[v];
else
swap(edge[i],edge[–m]);
}
n = tn;
root = id[root];
}
return res;
}
int g[MAXN][MAXN];

int a[100];
int sum[100];

int main()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
int n,m;
while(scanf(“%d%d”,&n,&m) == 2 ){
if(n == 0 && m == 0)break;
sum[0] = 0;
for(int i = 1;i <= n;i++){
scanf(“%d”,&a[i]);
sum[i] = sum[i-1] + a[i]+1;
}
int start = sum[n];
int tot = sum[n] + 1;
for(int i = 0;i < tot;i++)
for(int j = 0;j < tot;j++)
g[i][j] = INF;
for(int i = 0;i < n;i++)
g[start][sum[i]] = 0;
int c1,d1,l1,l2,mon;
while(m–){
scanf(“%d%d%d%d%d”,&c1,&l1,&d1,&l2,&mon);
int u = sum[c1-1]+l1;
int v = sum[d1-1]+l2;
g[u][v] = min(g[u][v],mon);
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= a[i];j++)
g[sum[i-1]+j][sum[i-1]+j-1] = 0;
}
int L = 0;
for(int i = 0;i < tot;i++)
for(int j = 0;j < tot;j++)
if(g[i][j] < INF){
edge[L].u = i;
edge[L].v = j;
edge[L++].cost = g[i][j];
}
int ans = gao(start,tot,L,edge);
printf(“%d\n”,ans);
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 358.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%