GGS-DDU HDU

GGS-DDU

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


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
 
 
把没门课分成1 - a[i] 个等级  每个等级i都向i - 1 连一条边 表示 如果 当前达到了等级i  则 1 - (i - 1) 都相当于达到了
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 100010, INF = 0x7fffffff;
int ID[maxn], IN[maxn], vis[maxn], pre[maxn];
int cnt;

struct node
{
    int u, v, c;
}Node[maxn*2];

void add(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt++].c = c;

}

int dirmst(int root, int n, int m)
{
    int ans = 0;
    while(true)
    {
        for(int i=0; i<n; i++) IN[i] = INF; //记录最小前驱边的值

        //1、找最小前驱边
        for(int i=0; i<m; i++)
        {
            int u = Node[i].u;
            int v = Node[i].v;
            if(Node[i].c < IN[v] && u != v)
            {
                pre[v] = u;
                IN[v] = Node[i].c;
            //    cout<< e.v << "     " << e.u <<endl;
            }
        }

        //2、判断是否联通
        for(int i=0; i<n; i++)
        {
            if(i == root) continue;
            if(IN[i] == INF) return -1;
        }

        //3、找环
        int cntnode = 0;
        mem(ID, -1);
        mem(vis, -1);
        IN[root] = 0;
        for(int i=0; i<n; i++)
        {
            ans += IN[i];
            int v = i;
            while(vis[v] != i && ID[v] == -1 && v != root)
            {
                vis[v] = i;
                v = pre[v];
            }
            //如果存在环 则把环中的点缩为一个点
            if(v != root && ID[v] == -1)
            {
                for(int j=pre[v]; j!=v; j=pre[j])
                {
                    ID[j] = cntnode;
                }
                ID[v] = cntnode++;
            }
        }
        if(cntnode == 0) break;  //没有环就结束

        //重新标记其它点
        for(int i=0; i<n; i++)
            if(ID[i] == -1)
                ID[i] = cntnode++;
        for(int i=0; i<m; i++)
        {
            int v = Node[i].v;
            Node[i].u = ID[Node[i].u];
            Node[i].v = ID[Node[i].v];
            if(Node[i].u != Node[i].v)
                Node[i].c -= IN[v];
        }
        n = cntnode;
        root = ID[root];
    }
    return ans;

}

int sum[maxn], a[maxn], d, c, L1, L2;
int w, s;

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        if(n == 0 && m == 0) break;
        mem(sum, 0);
        cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            a[i]++;
            sum[i] = sum[i - 1] + a[i];
        }
        for(int i = 1; i <= m; i++)
        {
            //cin >> c >> L1 >> d >> L2 >> w;
            scanf("%d%d%d%d%d", &c, &L1, &d, &L2, &w);
            L1++, L2++;
            add(sum[c - 1]  + L1, sum[d - 1] + L2, w);
        }
        s = 0;
        for(int i = 1; i <= n; i++)
        {
            add(s, sum[i - 1] + 1, 0);
            for(int j = a[i]; j >= 2; j--)
                add(sum[i - 1] + j, sum[i - 1] + j - 1, 0);
        }
        int ans = dirmst(s, sum[n] + 1, cnt);
        if(ans < 0)
            printf("-1
");
        else
            printf("%d
", ans);


    }


    return 0;
}
 

GGS-DDU

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


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
 
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
原文地址:https://www.cnblogs.com/WTSRUVF/p/10651688.html