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, 那么需要满足的 ...
随机推荐
- Private Variable and Private Method - Python 私有变量 和 私有方法
Private Variable and Private Method Python 不象 Java 那样, 通过 private 关键字创建私有属性, python 通过更简洁的实现了'私有属性', ...
- k8s系列---pod介绍
# yaml格式的pod定义文件完整内容: apiVersion: v1 #必选,版本号,例如v1 kind: Pod #必选,Pod metadata: #必选,元数据 name: string # ...
- ASP.NET Core ResponseCache进行缓存操作
前言 本章将介绍客户端缓存将介绍浏览器缓存和服务端缓存,使用浏览器缓存将减少对web服务器的请求次数,同时可以提升性能,避免重复的运算浪费. ASP.NET Core对于HTTP缓存分为两种: 客户端 ...
- jQuery-Moblie在Chrome下出现的问题
第一次用jQuery然后就遇到很蛋疼的地方,打开页面一直处在菊花状态,一开始以为自己搞错什么,是不是引用错文件,看里面的错误警告 Failed to execute 'replaceState' on ...
- F——宋飞正传(HDU3351)
题目: I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simpl ...
- JavaWeb学习(四) : JSP 基本语法
引入 JSP 的概念: 通俗来讲 : 在 HTML 代码中混合 Java 代码能让网页具有动态的功能, 而嵌入了 Java 代码的网页就是 JSP. JSP 的特点(优点) : 1.运行于服务端(是一 ...
- .NET Core 3 Web Api Cors fetch 一直 307 Temporary Redirect
.NET Core 3 Web Api Cors fetch 一直 307 Temporary Redirect 继上一篇 .net core 3 web api jwt 一直 401 为添加JWT- ...
- linux系统中运行node进程,无法杀死进程
events.js:72 throw er; // Unhandled 'error' event ^Error: listen EADDRINUSE at errnoException (net.j ...
- HA: Chakravyuh Vulnhub Walkthrough
靶机链接: https://www.vulnhub.com/entry/ha-chakravyuh,388/ 主机探测扫描: 端口扫描: ╰─ nmap -p- -sC -sV 10.10.202.1 ...
- python命令行运行django项目, can't open file 'manage.py' 问题解决
找到manage.py的绝对路径即可运行