Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >POJ 3159 Candies(SPFA+栈)差分约束

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

作者头像
全栈程序员站长
发布于 2022-02-05 08:09:46
发布于 2022-02-05 08:09:46
20600
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是全栈君。

题目链接:http://poj.org/problem?id=3159

题意:给出m给 x 与y的关系。当中y的糖数不能比x的多c个。即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖?

差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,就可以 

有负环时,用栈比队列快

数组开小了,不报RE,报超时 ,我晕

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
const int N = 210;
const int maxn = 30100;
const int maxm = 200000;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define init(a) memset(a,0,sizeof(a))
#define MIN INT_MIN
#define MAX INT_MAX
#define LL long long
using namespace std;
int max(int a,int b){if(a>b)return a; else return b;}
int min(int a,int b){if(a<b)return a; else return b;}
const int INF=0x3f3f3f3f;
struct node
{
    int v,w;
    int next;
}edge[maxm];
int head[maxn];
bool vis[maxn];
int dis[maxn];
int cnt;
void add(int a,int b,int w)//加边
{
    edge[cnt].v=b;
    edge[cnt].w=w;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}
void SPFA(int s,int n)
{
    //stack<int>stk;
    int stk[100000];
    int top = 0;
  
    //stk.push(s);
    stk[top++] = s;
    FOR(i,1,n+1)
    {dis[i] = INF;
    vis[i] = 0;
    }
    dis[s] = 0;
    vis[s] = 1;
    while(top!=0)
    {
        int u=stk[--top];
        //stk.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].w)
            {
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    stk[top++] = v;
                }
            }
        }

    }
}
void initt()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
int main()
{
    int n,m;
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        initt();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        SPFA(1,n);
        printf("%d\n",dis[n]);
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115400.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
图论--差分约束--POJ 2983--Is the Information Reliable?
The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straight line with N defense stations. Because of the cooperation of the stations, Zibu’s Marine Glory cannot march any further but stay outside the line.
风骨散人Chiam
2020/10/28
3540
POJ 2449 Remmarguts' Date(k短路模板)
       题意是n个点,m条边,然后输入m条边,最后输入起始点和终止点和第k短路。
Ch_Zaqdt
2019/01/10
5630
图论--差分约束--POJ 3169 Layout(超级源汇建图)
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).Description
风骨散人Chiam
2020/10/28
2520
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
意甲冠军:给定一个有向图有m单向边缘。免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求
全栈程序员站长
2022/07/05
1760
POJ1201 Intervals(差分约束)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.  Write a program that:  reads the number of intervals, their end points and integers c1, ..., cn from the standard input,  computes the minimal size of a set Z of in
attack
2018/04/10
8840
图论--差分约束--POJ 1201 Intervals
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n, writes the answer to the standard output.
风骨散人Chiam
2020/10/28
3060
POJ 3259 Wormholes(spfa判负环)
       题意是有n个点,m条边,k个虫洞(权值为负),输入完m条无向边后输入k条有向边,问能不能找到一个点,从这个点出发,最后回到这个点的时候权值是负的(时光倒流)。
Ch_Zaqdt
2019/01/10
7990
POJ1275 Cashier Employment(差分约束)
Description A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its need. The supermarket manager has hired you to help him, solve his problem. The problem is that the supermarket needs different number of cashier
attack
2018/04/10
6400
网络流详解(流网图一般能够反映什么信息)
https://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html
全栈程序员站长
2022/08/02
9610
网络流详解(流网图一般能够反映什么信息)
图论--差分约束模板
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define INF 1e9 using namespace std; const int maxn=1000+10; const int maxm=50000+10; struct Edge { int from,to,dist; Edge(){} Edge(int f,int t,int d):from(f),to(t),dist(d
风骨散人Chiam
2020/10/28
2750
Tricks Device (hdu 5294 最短路+最大流)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 124 Accepted Submission(s): 27
全栈程序员站长
2022/07/10
2220
差分约束题解
用户11039529
2024/05/26
730
差分约束题解
图论算法模板整理及思路 不断更新 绝对精品
DFS 1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 using namespace std; 5 queue<int>q; 6 int map[1001][1001]; 7 int vis[1001]; 8 int n,m; 9 void bfs(int p) 10 { 11 q.push(p); 12 vis[p]=1; 13 printf("%c-->",char(q.front(
attack
2018/04/12
8420
spfa(链式前向星)+dijkstra(链式前向星)
链式前向星可以存图, 它存图的方式是: 将 任 意 一 个 节 点 的 所 有 临 边 按 输 入 顺 序 依 次 连 接 起 来 将任意一个节点的所有临边按输入顺序依次连接起来 将任意一个节点的所有临边按输入顺序依次连接起来 然 后 头 节 点 ( 数 组 ) 存 的 是 最 后 一 个 临 边 的 地 址 然后头节点(数组)存的是最后一个临边的地址 然后头节点(数组)存的是最后一个临边的地址
全栈程序员站长
2022/11/17
5030
一次讲透次短路及条数问题,详细探讨dijkstra算法的本质
继续学习次短路~ hdu 3191 How Many Paths Are There
ACM算法日常
2019/11/06
1.8K0
一次讲透次短路及条数问题,详细探讨dijkstra算法的本质
图论--最长路--基于SPFA的调整模板
#include<iostream> #include<queue> #include<algorithm> #include<set> #include<cmath> #include<vector> #include<map> #include<stack> #include<bitset> #include<cstdio> #include<cstring> #define Swap(a,b) a^=b^=a^=b #define cini(n) scanf("%d",&n) #define cinl
风骨散人Chiam
2020/10/28
5060
《图》相关刷题
用户11039529
2024/04/10
880
The 2019 Asia Nanchang First Round Online Programming Contest B Fire-Fighting Hero(阅读理解)
This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are VV (Numbers 11 to VV) fire-fighting points in ACM city. These fire-fighting points have EE roads to communicate with each other. Among them, there is a fire-fighting hero in the SS fire-fighting point, and the fire-fighting team is distributed in K fire-fighting points. If a fire-fighting point needs to be put out, the fire-fighting hero or the fire-fighting team must arrive as soon as possible, that is, to choose the shortest route to arrive.
风骨散人Chiam
2020/10/28
2620
P3831 [SHOI2012]回家的路 分层最短路
每一站需要2分钟,转向需要1分钟。将每个点变成两个点,一个点负责跑横向路,一个点负责跑纵向路。然后想象一层图变成了两层,其中一层只有横向,另一层只有竖向。两层图转换的代价是1,也就是换乘。
用户2965768
2019/04/09
3570
[HNOI2005]狡猾的商人 差分约束+判环
定的sum[u→v]=c 不仅需要满足sum[v]−sum[u−1]=c,还应该满足sum[u−1]−sum[v]=−c。
用户2965768
2019/04/18
3440
推荐阅读
相关推荐
图论--差分约束--POJ 2983--Is the Information Reliable?
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验