//
#include<iostream>
#include<cstring>
#include<queue>
#include<stack>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=,M=;
int h[N],e[M],ne[M],w[M],idx;
int st[N];
int dist[N];
int n,m;
bool vis[N];
int Scan()
{
int res = , ch, flag = ;
if((ch = getchar()) == '-') //判断正负
flag = ;
else if(ch >= '' && ch <= '') //得到完整的数
res = ch - '';
while((ch = getchar()) >= '' && ch <= '' )
res = res * + ch - '';
return flag ? -res : res;
}
void add(int a,int b,int c)
{
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
void spfa(int start)
{
memset(vis,,sizeof vis);
for(int i=;i<=n;i++)
dist[i]=INF;
stack<int>s;
s.push(start);
dist[start]=;
vis[start]=;
while(s.size())
{
int u=s.top();
s.pop();
vis[u]=;
for(int j=h[u];j!=-;j=ne[j])
{
int v=e[j];
int w1=w[j];
if(dist[v]>dist[u]+w1)
{
dist[v]=dist[u]+w1;
if(vis[v]==)
{
s.push(v);
vis[v]=;
}
}
}
}
printf("%d\n",dist[n]);
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
idx=;
memset(h,-,sizeof h);
for(int i=;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
}
spfa();
}
return ;
}

Candies POJ - 3159 差分约束的更多相关文章

  1. poj 3159(差分约束经典题)

    题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...

  2. poj 3159 差分约束

    思路:班长的糖果要比snoopy的多.并且要用手写堆栈,且堆栈的大小要开到20000000. #include<iostream> #include<cstdio> #incl ...

  3. (简单) POJ 3159 Candies,Dijkstra+差分约束。

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

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

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

  5. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  6. poj 1201 差分约束

    http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...

  7. POJ - 3169 差分约束

    题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如         果最大距离无限大则输出 ...

  8. POJ 1201 差分约束+SPFA

    思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...

  9. Candies POJ - 3159

    题目链接:https://vjudge.net/problem/POJ-3159 思路: 能看出是差分约束的题, 我们想假设一个人是 p(1),另一个人是p(2),他们之间糖果差为w, 那么需要满足的 ...

随机推荐

  1. 【笔记】最短路——SPFA算法

    ##算法功能 找最短路(最长路?) ##算法思想 用一个节点k更新节点i到节点j的最短路 ##邻接链表存储 基础而高效的图的存储方式 存的是单向边(无向边可以看成两条有向边) ##实现 维护节点i到源 ...

  2. 重读es6, 正确了解promise中catch的用法

    前言 在最近的项目中,用到了es6的promise语法,发现promise.prototype.catch 并不只是单单reject抛出的回调函数,所以今天做一些笔录,防止以后在项目中又碰到这样的问题 ...

  3. qt creator源码全方面分析(2-3-2)

    目录 Showing Task List Files in Issues Pane 管理任务列表条目 任务列表文件格式 Showing Task List Files in Issues Pane 您 ...

  4. RTEMS进程同步机制

    互斥量 好像没有互斥量,信号量接收那儿有个图,互斥量似乎术语一类特殊的信号量. 信号量 12. Semaphore Manager 12.1. Introduction The semaphore m ...

  5. CodeForces 327B 水题。

    I - 9 Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  6. 图像分割:Semantic/Instance/Panoramic Segmentation

    一. 背景介绍 语义分割(Semantic Segmentation):对一张图片上的所有像素点进行分类,同一物体的不同实例不需要单独分割出来. 实例分割(Instance Segmentation) ...

  7. npm/gulp/nodejs

    npm淘宝镜像:https://npm.taobao.org/ vscode先安装npm淘宝镜像 再安装gulp:https://www.cnblogs.com/xiaoleiel/p/1116056 ...

  8. 「Flink」使用Managed Keyed State实现计数窗口功能

    先上代码: public class WordCountKeyedState { public static void main(String[] args) throws Exception { S ...

  9. python——面向对象(3),搬家具

    """date: 2020.2.9搬家具:将小于房子剩余面积的家具搬进房子1.定义家具类,房屋类""" class Furniture(): ...

  10. linux centos7安装mysql8

    一.RPM版安装 查看是否有其他版本的数据库,若有,删除干净 非root用户必须要有sudo权限 1.下载mysql相关安装包 https://mirrors.tuna.tsinghua.edu.cn ...