HDU 4966 GGS-DDU 最小树形图

GGS-DDU

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


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
 

Source
 




比赛时根本没看这道题,结束后自己写写1Y。。。。


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-15

typedef long long ll;
typedef unsigned long long ull;

const int mn=5555;
const int mm=mn*mn;

struct edge{
    int u,v;
    double cost;
}e[mm];

double In[mn];
int ID[mn];
int vis[mn];
int pre[mn];
int n,m;

double Directed_MST(int root)
{
    double ret=0;
    int u,v;
    while(true)
    {
        for(int i=0;i<n;i++)
         In[i]=INF;
        for(int i=0;i<m;i++)
        {
            u=e[i].u;
            v=e[i].v;
            if(e[i].cost<In[v] && u!=v)
            {
                In[v]=e[i].cost;
                pre[v]=u;
            }
        }
        for(int i=0;i<n;i++)
        {
            if(i==root)
               continue;
            if(In[i]==INF)
               return -1;
        }

        int cnt=0;
        MST(ID,-1);
        MST(vis,-1);
        In[root]=0;

        for(int i=0;i<n;i++)
        {
            ret+=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(u=pre[v];u!=v;u=pre[u])
                {
                    ID[u]=cnt;
                }
                ID[v]=cnt++;
            }
        }
        if(cnt==0)
            break;
        for(int i=0;i<n;i++)
        {
            if(ID[i]==-1)
              ID[i]=cnt++;
        }
        for(int i=0;i<m;i++)
        {
            v=e[i].v;
            e[i].u=ID[e[i].u];
            e[i].v=ID[e[i].v];
            if(e[i].u!=e[i].v)
            {
                e[i].cost-=In[v];
            }
        }
        n=cnt;
        root=ID[root];
    }
    return ret;
}

int a[55];
map< pair< int, int >,int> p;

int main()
{
    int N,M;
    while(cin>>N>>M)
    {
        if(!N && !M) break;
        n=1;
        m=0;
        p.clear();
        int cnt=1;
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&a[i]);
            n+=a[i];
            for(int j=1;j<=a[i];j++)
            {
                if(j==1)
                {
                    p[MP(i,j)]=cnt++;
                    e[m].u=j,e[m].v=0,e[m].cost=0;
                    m++;
                }
                else
                {
                    p[MP(i,j)]=cnt++;
                    e[m].u=cnt-1,e[m].v=cnt-2,e[m].cost=0;
                    m++;
                }
            }
        }
        int c,l1,d,l2,w;
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d%d%d",&c,&l1,&d,&l2,&w);
            int u=p[MP(c,l1)];
            int v=p[MP(d,l2)];
            e[m].u=u,e[m].v=v,e[m].cost=w;
            m++;
        }
        int ans=Directed_MST(0);
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值