Candies POJ - 3159 差分约束
//
#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 差分约束的更多相关文章
- poj 3159(差分约束经典题)
题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...
- poj 3159 差分约束
思路:班长的糖果要比snoopy的多.并且要用手写堆栈,且堆栈的大小要开到20000000. #include<iostream> #include<cstdio> #incl ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- Candies POJ - 3159 (最短路+差分约束)
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- poj 1201 差分约束
http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...
- POJ - 3169 差分约束
题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如 果最大距离无限大则输出 ...
- POJ 1201 差分约束+SPFA
思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...
- Candies POJ - 3159
题目链接:https://vjudge.net/problem/POJ-3159 思路: 能看出是差分约束的题, 我们想假设一个人是 p(1),另一个人是p(2),他们之间糖果差为w, 那么需要满足的 ...
随机推荐
- 【OpenGL】变换矩阵计算公式
摘自: http://ogldev.atspace.co.uk/www/tutorial06/tutorial06.html, http://ogldev.atspace.co.uk/www/tuto ...
- 使用docker19.03.6部署zabbix
可参考官方文档:https://www.zabbix.com/documentation/4.0/zh/manual/installation/containers 1)启动一个空的mysql服务器实 ...
- BeautifulSoup入门
BeautifulSoup库入门 BeautifulSoup库的理解 BeautifulSoup库是解析.遍历.维护”标签树”的功能库 示例代码: from bs4 import BeautifulS ...
- pycharm创建Django项目时报 AttributeError:'module' object has no attrbute 'main' 错误或者创建了就只有venv一个目录
这是因为创建项目时候没有选择合适的项目环境. 所以在创建项目的时候选择一下项目的环境,比如选择python的运行环境 这时候创建的项目就不再报 AttributeError:'module' obje ...
- apache/tomcat安装过程略
apache/tomcat安装过程略 一些变量 apache安装目录 APACHE_PREFIX=/Data/app/apache apache配置文件 APACHE_CONF=/etc/httpd/ ...
- JSON Hijacking实战利用
0×01漏洞的挖掘 一般挖掘的过程中,burpsuite代理的History做寻找,过滤多余不可能存在漏洞的一些链接,如下图所示: 我们在返回包中json格式发现了如下的敏感信息(用户Id,用户名,用 ...
- Android Studio 3.6 正式版终于发布了
Google 下载地址 百度云 下载地址 密码:epl9 如题,Android Studio 3.6 正式版终于发布了,值得兴奋呀,毕竟 3.5 大版本更新也已经差不多半年了,撒花撒花!这次更新又更新 ...
- ES6学习笔记(一):轻松搞懂面向对象编程、类和对象
目录 面向过程编程P OP(Process oriented programming) 面向对象编程OOP(Object Oriented Programming) 总结 @ 面向过程编程P OP(P ...
- Java连载89-SorteSet、Comparable接口
一. SortedSet集合直接举例 package com.bjpowernode.java_learning; import java.util.*; /** * java.util.Set * ...
- DOTNET Core MVC(二)路由初探
搁置了几天,工作忙的一塌糊涂,今天终于抽空来继续看看MVC的知识.先来看看MVC的路由是如何处理的.以下为替代的路由: app.UseEndpoints(endpoints => { endpo ...