[USACO 05 DEC]Cleaning Shifts

Description face questions

John cows pampered childhood, they can not tolerate any dirty things in the bullpen. John found that if you want this group got a little older cows satisfaction, he had to hire some of them to clean the bullpen, John cows have \ (N (1 \ leq N \ leq 10000) \) head willingness by cleaning bullpen to earn some pocket money.

Because cows littering will naturally anywhere in the cowshed in a certain period, they are required at this time, no matter what time must have at least one cow was cleaning. Needs cleaning period of from one day \ (M \) seconds starts, the section \ (E \) second end \ ((0 \ M Leq \ Leq E \ Leq 86399) \) . Note here refers to the time period in seconds, rather than the time point, i.e., the total time required to clean every day \ (E-M + 1 \ ) seconds.

John has been a work plan they are willing to accept from each cow there: For a cow, she is willing sleeping mat in a day \ (T_1 \ ldots T_2 \) second time period work \ ((M \ leq T_1 \ leq T_2 \ Leq E) \) , is required to pay \ (S \) to $ \ ((0 \ Leq S \ Leq 500000) \) . And the need to clean the described period, as if a cow is willing to work period per day of the first \ (10 \ ldots 20 \) seconds, that time her total work is \ (11 \) seconds, instead of \ (10 \) second

Once John decided to hire a cow, you have to pay her full salary, not just let her work for some time, then press this time in the total time she was willing to work to determine a percentage of her salary . Now you help determine which cows John hired to keep the bullpen clean, of course, under the premise that allows cows satisfactory, John hopes to make the total cost as small as possible.

Input Format

The first \ (1 \) line: \ (3 \) positive integers \ (N, M, E \) .
Second \ (2 \) to (N + 1 \) \ : Line \ (i + 1 \) line gives the number \ (I \) work plan cows, i.e. \ (3 \) positive integers \ (T_1, T_2, S \) .

Output Format

Output An integer that represents the least cost to pay for John bullpen cleanup. If the clean-up work impossible, then the output \ (--1 \) .

Sample

Sample input

3 0 4
0 2 3
3 4 2
0 0 1

Sample Output

5

answer

You can directly use the greedy approach, but here I use the idea of graph theory is calculated.
Each time a set of one point per cow from \ (I \) work to \ (J \) i.e. from point \ (I \) point \ (J \) is a directed edge. While at the same time work because cows can cross, we should take every point \ (p \) as a starting point to \ (p-1 \) and even a reverse side. Finally, we come to the end directly to a short circuit from the starting point to the most

#include<iostream>
#include<set>
#include<algorithm>
#include<cstdio>
#define maxn 100000
#define maxm 100000
#define X first
#define Y second
using namespace std;
inline char get(){
    static char buf[30000],*p1=buf,*p2=buf;
    return p1==p2 && (p2=(p1=buf)+fread(buf,1,30000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    register char c=get();register int f=1,_=0;
    while(c>'9' || c<'0')f=(c=='-')?-1:1,c=get();
    while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=get();
    return _*f;
}
typedef pair<int,int> pall;
struct edge{
    int u,v,w,next;
}E[maxm<<1];
int p[maxn],eid=0;
struct cmp{
    bool operator ()(const pall &a,const pall &b)const{
        if(a.Y!=b.Y)return a.Y<b.Y;
        return a.X<b.X; 
    }
};
void init(){
    for(register int i=0;i<maxn;i++)p[i]=-1;
    eid=0; 
}
void insert(int u,int v,int w){
    E[eid].u=u;
    E[eid].v=v;
    E[eid].w=w;
    E[eid].next=p[u];
    p[u]=eid++;
}
void insert2(int u,int v,int w){
    insert(u,v,w);
    insert(v,u,w);
}
int n,m,e;
int dis[maxn],vis[maxn];
void dijkstra(int u){
    for(register int i=0;i<maxn;i++)dis[i]=0x3f3f3f3f,vis[i]=0;
    dis[u]=0;vis[u]=1;
    set<pall,cmp> s;
    s.insert(make_pair(u,0));
    for(register int i=0;i<=n || s.size();i++){
        u=s.begin()->X;
        s.erase(*s.begin());
        vis[u]=1;
        for(register int j=p[u];~j;j=E[j].next){
            int v=E[j].v;
            int w=E[j].w;
            //cout<<u<<" "<<v<<" "<<w<<endl;
            if(!vis[v] && dis[v]>dis[u]+w){
                s.erase(make_pair(v,dis[v]));
                s.insert(make_pair(v,dis[v]=dis[u]+w));
            }
        } 
    }
}
int u,v,w;
int ss[maxn],tt[maxn];
int main(){
    //freopen("1.txt","r",stdin);
    init();
    n=read(),m=read(),e=read();
    //cout<<n<<" "<<m<<" "<<" "<<e<<endl;
    for(register int i=1;i<=n;i++){
        int s=read(),t=read(),w=read();
        insert2(s,t,w);
        insert2(s,t+1,w);
        //cout<<s<<" "<<t<<" "<<w<<endl;
    }
    for(register int i=m;i<=e;i++)insert(i+1,i,0);
    dijkstra(m);
    //for(register int i=1;i<=e;i++)cout<<dis[i]<<" ";
    if(dis[e+1]!=0x3f3f3f3f)cout<<dis[e+1];
    else cout<<-1<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Chen574118090/p/11608689.html