题目链接:https://vjudge.net/problem/POJ-3159

思路:

能看出是差分约束的题,

我们想假设一个人是 p(1),另一个人是p(2),他们之间糖果差为w,

那么需要满足的是 :  p(2) - p(1) <= w,

为了让p(1) 和 p(n)差距最大,我们可以取w,为了满足题目要求

p2 - p1 <= w1, p3 - p2 <= w2,  p3 - p1 <= w3 ... ...  px - py <= wn(举例是任意的两个边要满足),

我们可以建图了,用spfa的话,可以把松弛条件改了,

if(dist[v] - dist[u] > w)  说明不合题目意思了,那么

dist[v] = dist[u] + w;  去更新他。

这里用队列优化spfa不可以,会超时,用栈可以,这里我认为是,栈类似于dfs,一个点的其他情况走到底,

相比于队列类比bfs宽搜,每个点和边都要遍历到,用类似于dfs的方法可以减少其他宽搜的分支。

而且,而且,而且,C++会超时。。。关了输入输出同步也会,c在大量数据输入时,c++还是比不了啊。。。


 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf 1e9
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
int head[N];
int vis[N];
int dist[N];
stack<int> sta;
int cnt;
int n,m;
bool ok; struct Edge{
int to;
int w;
int next;
}e[]; inline void add(int u,int v,int w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void SPFA(){ rep(i,,n) dist[i] = inf;
dist[] = ;
sta.push(); while(!sta.empty()){
int u = sta.top();
sta.pop();
vis[u] = false; for(int o = head[u]; ~o; o = e[o].next){
int v = e[o].to;
int w = e[o].w; if(dist[v] - dist[u] > w){
dist[v] = dist[u] + w;
if(!vis[v]){
vis[v] = true;
sta.push(v);
}
}
}
} printf("%d\n",dist[n]);
// cout << dist[n] << endl;
} int main(){ // ios::sync_with_stdio(false);
// cin.tie(0); scanf("%d%d",&n,&m);
// cin >> n >> m;
rep(i,,n) head[i] = -;
cnt = ; int u,v,w;
rep(i,,m){
// cin >> u >> v >> w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
} SPFA(); getchar(); getchar();
return ;
}

Candies POJ - 3159的更多相关文章

  1. Candies POJ - 3159 (最短路+差分约束)

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  2. Candies POJ - 3159 差分约束

    // #include<iostream> #include<cstring> #include<queue> #include<stack> #inc ...

  3. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  4. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  5. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  6. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  7. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  8. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  9. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

随机推荐

  1. zr2019暑期高端峰会AB组十测

    郑睿round 1 代码真的好写,还是太笨了,爆零了. 郑睿round_2 好失败,A题真的是日狗了,第一发就可以A的,忘记费用流的反向边也要加一发流量了.哎,我注定是要爆零. 正睿round_3 日 ...

  2. NLP之预训练

    内容是结合:https://zhuanlan.zhihu.com/p/49271699 可以直接看原文 预训练一般要从图像处理领域说起:可以先用某个训练集合比如训练集合A或者训练集合B对这个网络进行预 ...

  3. 基于paramiko将文件上传到服务器上

    通过安装使用paramiko模块,将本地文件上传到服务器上 import paramiko import datetime import os hostname = '服务器ip' username ...

  4. [LeetCode] 900. RLE Iterator RLE迭代器

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  5. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  6. [******] java多线程连续打印abc

    题目描述 建立三个线程A.B.C,A线程打印10次字母A,B线程打印10次字母B,C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印. 5种方法 使 ...

  7. .NET Core创建Worker Services

    .NET CORE 3.0新增了Worker Services的新项目模板,可以编写长时间运行的后台服务,并且能轻松的部署成windows服务或linux守护程序.如果安装的vs2019是中文版本,W ...

  8. 【RS】Deep Learning based Recommender System: A Survey and New Perspectives - 基于深度学习的推荐系统:调查与新视角

    [论文标题]Deep Learning based Recommender System: A Survey and New Perspectives ( ACM Computing Surveys  ...

  9. ScheduledThreadPoolExecutor源码

    添加元素,先添加到数组末尾,然后上调整堆. 取对首元素,把最后一个元素放到0位置,然后下调整堆. 移除中间元素,把最后一个元素放到中间位置,然后下调整堆,下调整堆没动(已经是最大的),就在上调整堆.下 ...

  10. update改数据详解

    update修改数据的要素  : 改哪张表? 改哪几列的值? 分别改成什么值? 在哪些行生效?(这个很重要,否则所有行都会受影响) mysql> update class ; where 表达式 ...