FJUT 1231 最短路径

Dsc最大的梦想就是有一款属于自己的游戏(不可能的),可以把自己的奇思妙想在虚拟的世界中创造出来,假设Dsc成功了,创造出了一款网游(小作坊的那种),现在为了节省成本,Dsc决定自己维护服务器健康,但总会有没时间的时候。

假设从S分钟开始,E分钟结束[S,E]这段时间Dsc是没有时间的,这时候Dsc需要找人来维护服务器,当然是有偿的。在[S,E]这段时间中有n个人有空余时间(不一定是全部[S,E])第i个人在ai,bi这段时间有空,共需要ci的管理费。

现在请帮Dsc算一算,在他没空的时间内[S,E],每天都至少有一个人在管理服务器所需要的最少花费是多少,如果没有任何方案使[S,E]时间内每天都至少有一个人在管理服务器则输出-1;

Input
第一行为三个整数n,S,E分别表示有n个人有空,在[S,E]时间内Dsc没空

接下来有n行,每行3个整数ai,bi,ci分别表示第i个人在[ai,bi]时间内有空,雇佣共需要ci管理费

1<=n<=1e5

0<=S<=E<=1e5

S<=ai<=bi<=E

1<=ci<=1e5

Output
Dsc在没空的时间找人管理服务器所需的最少花费,没有方案则输出-1

SampleInput
3 2 4
2 3 2
4 4 1
2 4 4
SampleOutput
3
题意:给你一个时间区间 并给出了该时间区间的n个带权子区间 问消耗最少的权值将区间内的点完全覆盖
思路:这题正解是一个线段树+dp,But我这题一看到就想到了最短路径 但是最短路覆盖的是区间 而这题要求的点 比赛的时候我只想到了将给定的子区间的右端点延伸一位(对的),并将间隔为1的点建一条边权为0的边(错的),但这个思路前者是对的,后面就WA了,连样例都跑不对 ,后面看题解知道了图原来还可以这么建orzzz,延伸一位后,我们再将左端点减小一位,建一条边权为0的边,使其可以遍历任意n个子区间,通俗点说就是可以倒回来走别的边,具体看代码

扫描二维码关注公众号,回复: 10173489 查看本文章
#include <stdio.h>
#include <string.h>
#include <queue>
#include <deque>
#include <vector>

using namespace std;
typedef long long LL;///这题会爆int

const int maxn=1e5+5;
const int inf=0x3f3f3f3f;

struct edge
{
    LL to,w;
    edge() {}
    edge(LL tt,LL ww):to(tt),w(ww) {};
};

vector<edge>a[maxn];

struct node
{
    LL to,cost;
    node() {}
    node(LL tt,LL cc):to(tt),cost(cc) {};
    friend bool operator < (const node &a,const node &b)
    {
        return a.cost > b.cost;
    }
};

LL dist[maxn],vis[maxn];

int n,m,s,t;

void init()
{
    for(int i=0; i<=n; i++)
    {
        a[i].clear();
    }
}

LL dj(int s,int t)
{
    for(int i=0; i<=n; i++)
    {
        dist[i]=inf;
        vis[i]=0;
    }

    priority_queue<node>que;
    que.push(node(s,0));

    while(!que.empty())
    {
        node now=que.top();
        que.pop();

        if(!vis[now.to])
        {
            vis[now.to]=1;
            dist[now.to]=now.cost;

            for(int i=0; i<a[now.to].size(); i++)
            {
                LL t=a[now.to][i].to;
                LL c=a[now.to][i].w;
                if(!vis[t])
                {
                    que.push(node(t,c+now.cost));
                }
            }

        }
    }
    return dist[t]==inf ? -1:dist[t];
}
int main()
{
    while(~scanf("%d%d%d",&n,&s,&t))
    {
        init();

        for(int i=0; i<n; i++)
        {
            int x,y,v;
            scanf("%d%d%d",&x,&y,&v);
            a[x].push_back(edge(y+1,v));///给子区间向右伸长一位
        }

        for(int i=s+1;i<=n;i++)
        {
            a[i].push_back(edge(i-1,0));///建立反向边 使其可以遍历任意n个子区间 (有点像反悔)
        }
        
        LL ans=dj(s,t+1);
        printf("%lld\n",ans);

    }
    return 0;
}
发布了54 篇原创文章 · 获赞 0 · 访问量 1229

猜你喜欢

转载自blog.csdn.net/weixin_44144278/article/details/99248814