Candies POJ - 3159(差束约分理解 spfa+stack速度比queue+spfa 快)

本文探讨了一个特定的差分约束系统问题,在该问题中,我们需要找到分配糖果的最大差异,同时确保满足一系列不等式约束。文章提供了一种有效的方法来解决这个问题,即利用最短路径算法,并解释了为什么这种方法适用于此类问题。此外,还提供了详细的代码实现。


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.

附上第一篇推荐博客:(差束约分)

http://www.cnblogs.com/scau20110726/archive/2012/11/29/2794756.html

代码:


/*********************************************
PS:先看博客(差束约分)

        博客中的精华:

                 对于这种有一个未知数定死的差分约束系统,还有一个有趣的性质,
                 那就是通过最短路径算法求出来的一组解当中,所有未知数都达到最大值。

                 那么如果在一个未知数定死的情况下,要求其它所有未知数的最小值怎么办?只要反过来求最长路径就可以了。最长路径中的三角不等式与最短路径中相反:

                 d(v) >= d(u) + w(u, v)
                 也就是 d(v) - d(u) >= w(u, v)

关于差束约分的一点总结:(以本题为例)

            题目告诉了我们一个已知条件:b-a<=c;转化成 b<=a+c;对于这种模式,
            根据刚刚博客的中提到一点性质通过最短路算法求出的是最大值(对应题目的需求)
            所以直接套用最短路方法来做即可。

            强调一遍性质:
                        a<=b+c:最短路可以求得最大值
                        a>=b+c;最长路可以求得最小值。


            性质很重要! 性质很重要!性质很重要!!!

    最后一点:spfa + stack 优于 spfa + queue

**********************************************/
#include <cstdio>
#include<cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
#include <fstream>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include <iostream>

#define fo(i,a,b) for (int i=a;i<b;i++)
using namespace std;
const int maxn =1e6+5;
const int INF=0x3f3f3f3f;
typedef long long ll;
struct node
{
     int to;//终点
     int next;//下一条边的位置
     int w;
}e[maxn];
int cnt ;
bool vis[maxn];
int head[maxn],dist[maxn];
void add(int a,int b,int c)
{
     e[cnt].to=b;
     e[cnt].w=c;
     e[cnt].next=head[a];
     head[a]=cnt++;
}
bool yes ;//pan fu huan
int n,m;
int num[maxn];
void spfa(int st)
{
     fill(num,num+n+1,0);
     fill(vis,vis+n+1,0);
     fill(dist,dist+n+1,INF);
     int i,j,k;
        dist[st]=0;
        vis[st]=1;
        num[st]++;
//        queue<int>q;
        stack<int>q;
        q.push(st);
        while (!q.empty())
        {
             int u=q.top();
             q.pop();
             vis[u]=0;
             for (i=head[u];i!=-1;i=e[i].next)
             {
                 int qq=e[i].to;
                     if (dist[qq]>dist[u]+e[i].w)
                     {
                         dist[qq]=dist[u]+e[i].w;
                         if (!vis[qq])
                         {
                             num[qq]++;
                             vis[qq]=1;
                                q.push(qq);
                             if(num[qq]>n)
                             {
                                 yes=1;
                                 return ;
                             }
                         }
                     }
             }
        }
}
int main ()
{
//     std::ios::sync_with_stdio(0);
//     cin.tie(0);
//     cin>>n>>m;
        scanf("%d%d",&n,&m);
        int x,y,z;
        fill(head,head+n+1,-1);
     cnt=0;
     yes=0;
     fo (i,0,m)
     {
         scanf("%d%d%d",&x,&y,&z);
//         cin>>x>>y>>z;
         add(x,y,z);
     }
     spfa(1);
//     fo(i,1,n+1)
//     cout<<dist[i]<<" ";
//     cout<<endl;
     cout<<dist[n]<<endl;
    return 0;
}


内容概要:本文介绍了一个基于MATLAB实现的无人机三维路径规划项目,采用蚁群算法(ACO)与多层感知机(MLP)相结合的混合模型(ACO-MLP)。该模型通过三维环境离散化建模,利用ACO进行全局路径搜索,并引入MLP对环境特征进行自适应学习与启发因子优化,实现路径的动态调整与多目标优化。项目解决了高维空间建模、动态障碍规避、局部优陷阱、算法实时性及多目标权衡等关键技术难题,结合并行计算与参数自适应机制,提升了路径规划的智能性、安全性和工程适用性。文中提供了详细的模型架构、核心算法流程及MATLAB代码示例,涵盖空间建模、信息素更新、MLP训练与融合优化等关键步骤。; 适合人群:具备一定MATLAB编程基础,熟悉智能优化算法与神经网络的高校学生、科研人员及从事无人机路径规划相关工作的工程师;适合从事智能无人系统、自动驾驶、机器人导航等领域的研究人员; 使用场景及目标:①应用于复杂三维环境下的无人机路径规划,如城市物流、灾害救援、军事侦察等场景;②实现飞行安全、能耗优化、路径平滑与实时避障等多目标协同优化;③为智能无人系统的自主决策与环境适应能力提供算法支持; 阅读建议:此资源结合理论模型与MATLAB实践,建议读者在理解ACO与MLP基本原理的基础上,结合代码示例进行仿真调试,重点关注ACO-MLP融合机制、多目标优化函数设计及参数自适应策略的实现,以深入掌握混合智能算法在工程中的应用方法。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值