Candies (POJ3159) 线性约束差分+堆优化Dijkstra 最短路专题

题目描述

During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

题目大意:有n个小朋友,给每个小朋友分糖果,给你m种关系,即输入 a b c,意味着b孩子最多比a孩子的糖果数量多c,即x[ a ] - x[ b ] >=c,给你m种这些关系,问你第n个孩子最多比第1个孩子的糖果数量多多少。

思路:这一类问题可以差分约束算法解决。

          差分约束算法:如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分            约束系统。

          过程:把每个不等式都化为形如  X [ i ] - X [ j ]<= a [ k ] 的形式,如 

        (1)方程给出:X[n-1]-X[0]>=T ,可以进行移项转化为: X[0]-X[n-1]<=-T。

        (2)方程给出:X[n-1]-X[0]<T, 可以转化为X[n-1]-X[0]<=T-1。

        (3)方程给出:X[n-1]-X[0]=T,可以转化为X[n-1]-X[0]<=T&&X[n-1]-X[0]>=T,再利用(1)进行转化即可

          最后进行建图,即每个公式都可以变成 一条  j -> i  权值为a[k] 的有向边。

          最后,如该题要求 X[ n ] - X[ 1 ] 的最大值,只需要以 1 为源点,跑一边最短路,在求与 点 n 的最短路即可。

          另外,由于该题数据较大,就需要采用 堆优化的Dijkstra。

代码:

#include<bits/stdc++.h>

#define ll long long
#define ull unsigned long long 
#define MOD 998244353 
#define INF 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))  
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int NUM = 30005;
int n,m,len=1;
int head[NUM];
struct edge{
   int from,to,w;
}e[150005];   //边数
struct s_node{
   int id,n_dis;
   s_node(int a,int b){id=a;n_dis=b;}
   bool operator < (const s_node &a) const
   {return n_dis>a.n_dis;}
};
void add(int u,int v,int w)
{ 
    e[len].from=v;
    e[len].w=w;
    e[len].to=head[u];
    head[u]=len++;
}
int Dijkstra(int s)
{
    int dis[NUM];
    bool vis[NUM];
    for(int i=1;i<=n;i++){
        dis[i]=INF;
        vis[i]=0;
    }
    dis[s]=0;
    priority_queue<s_node>Q;
    Q.push(s_node(s,dis[s]));
    while(!Q.empty())
    {
        int u=Q.top().id;
        Q.pop();
        if(vis[u])continue;
        vis[u]=1;
        for(int i=head[u];i;i=e[i].to){
            int v=e[i].from;
            //if(vis[v])continue;
            if(dis[v]>dis[u]+e[i].w){
               dis[v]=dis[u]+e[i].w;
               Q.push(s_node(v,dis[v]));
            }
        }
    }
    return dis[n];
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
       int a,b,c;
       scanf("%d %d %d",&a,&b,&c);
       add(a,b,c);
    }
    printf("%d",Dijkstra(1));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hachuochuo_/article/details/107587249
今日推荐