poj3159—Candies(差分规划+dijsktra)

本文探讨了一个经典的差分约束问题,即幼儿园糖果分配案例。通过建立数学模型,利用SPFA算法和Dijkstra算法解决如何在满足孩子们对糖果数量相对公平的要求下,最大化两个特定孩子之间的糖果数量差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Candies

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 38441 Accepted: 10826

Description

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?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2
1 2 5
2 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr

 

/*
POJ 3159

差分约束+SPFA
*/

/*
/*
给n个人派糖果,给出m组数据,每组数据包含A,B,c  三个数,
意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。
最后求n 比 1 最多多多少糖果。
【解题思路】
这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,
我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,
即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大,
所以这题可以转化为最短路来求。
这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用 spfa + stack

或者用邻接矩阵版的dijskstra算法
*/

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
/*
 * 使用优先队列优化Dijkstra算法
 * 复杂度O(ElogE)
 * 注意对vector<Edge>E[MAXN]进行初始化后加边
 */
const long long INF=1e18;
const int maxm=5300100;
const int maxn=200000+100;
typedef long long LL;
struct qnode
{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};
struct Edge
{
    int v,cost;
    int next;
};
Edge edge[maxm];
int tol;
int head[maxm];
bool vis[maxn];
LL dist[maxn];
void Dijkstra(int n,int start)//点的编号从1开始
{
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)dist[i]=INF;
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            int cost=edge[i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void add(int u,int v,int w)
{
    edge[tol].v=v;
    edge[tol].cost=w;
    edge[tol].next=head[u];
    head[u]=tol++;
}
void init(){
    memset(head,-1,sizeof(head));
    tol=0;
}
LL a[maxn],c[maxn];
LL shuchu[maxn];
int main()
{
    int cas=0;
    int t,tt,n,m,s,x;
    int aa,bb,cc;

        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&aa,&bb,&cc);
            add(aa,bb,cc);
        }
        Dijkstra(n,1);
        printf("%d\n",dist[n]);
    return 0;
}

代码2:spfa+栈:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int MAXN=30010;
const int MAXE=150010;
const int INF=0x3f3f3f3f;
int head[MAXN];//每个结点的头指针
int vis[MAXN];//在队列标志

int Q[MAXN];//堆栈
int dist[MAXN];

struct Edge
{
    int to;
    int v;
    int next;
}edge[MAXE];
int tol;
void add(int a,int b,int v)//加边
{
    edge[tol].to=b;
    edge[tol].v=v;
    edge[tol].next=head[a];
    head[a]=tol++;
}
void spfa(int start,int n)
{
    int top=0;
    for(int v=1;v<=n;v++)//初始化
    {
        if(v==start)
        {
            Q[top++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            vis[v]=false;
            dist[v]=INF;
        }
    }
    while(top!=0)
    {
        int u=Q[--top];
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].v)
            {
                dist[v]=dist[u]+edge[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q[top++]=v;
                }
            }
        }
    }
}
int main()
{
    int n,m,a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF){
            tol=0;//加边计数,这个不要忘
        memset(head,-1,sizeof(head));
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        spfa(1,n);
        cout<<dist[n]<<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值