POJ 3159 Candies(差分约束)
http://poj.org/problem?id=3159
题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径。
思路:这个题让会神给讲的,用的dijkstra,看的网上很多用SPFA的。
关于SPFA:http://www.cnblogs.com/devtang/archive/2011/08/25/spfa.html
http://blog.csdn.net/chenjiang492943457/article/details/5375413
关于差分约束系统:http://www.cnblogs.com/void/archive/2011/08/26/2153928.html
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue> using namespace std ;
const int maxn = ;
const int maxm = ;
int head[maxn],next[maxm] ;
int map[maxn] ;
bool vis[maxn] ;
const int INF = << ;
int cnt , n , m;
struct node
{
int u ;
int v ;
int c ;
node(){}
node(int u,int v,int c):u(u),v(v),c(c){} }edge[maxm] ; struct node1
{
int v,c ;
node1(){}
node1(int v,int c):v(v),c(c){}
bool operator <(const node1 &a)const
{
return c > a.c ;
}
} ;
void addnode(int u,int v,int c)
{
edge[cnt] = node(u,v,c) ;
next[cnt] = head[u] ;
head[u] = cnt++ ;
} ;
bool relax(int u,int v,int c)
{
if(map[v] > map[u] + c)
{
map[v] = map[u]+c ;
return true ;
}
return false ;
} void Init()
{
memset(head,-,sizeof(head)) ;
memset(next,-,sizeof(next)) ;
cnt = ;
for(int i = ; i < m ; i++)
{
int x,y,z ;
scanf("%d %d %d",&x,&y,&z) ;
addnode(x,y,z) ;
}
} void dija(int sh)
{
memset(vis,,sizeof(vis)) ;
for(int i = ; i <= n ; i++)
map[i] = INF ;
map[sh] = ;
priority_queue<node1>Q ;
Q.push(node1(sh,map[sh])) ;
for(int i = ; i < n ; i++)
{
while(!Q.empty() && vis[Q.top().v]) Q.pop() ;
if(Q.empty()) break ;
node1 temp = Q.top() ;
Q.pop() ;
vis[temp.v] = true ;
for(int j = head[temp.v] ; j != - ; j = next[j])
{
if(relax(temp.v,edge[j].v,edge[j].c) && !vis[edge[j].v])
Q.push(node1(edge[j].v,map[edge[j].v])) ;
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
Init() ;
dija() ;
printf("%d\n",map[n]) ;
}
return ;
}
POJ 3159 Candies(差分约束)的更多相关文章
- POJ 3159 Candies 差分约束dij
分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
随机推荐
- javascript for in 循环时,会取到Array.prototype
/** *删除数组指定下标或指定对象 */ if(!Array.prototype.remove){ Array.prototype.remove = function(obj){ for(var i ...
- .Net 把网页Html转PDF文件
.Net 把网页Html转PDF文件 此篇主要利用 wkhtmltopdf 进行转换. 一.控制台直接转换 首先到官网http://wkhtmltopdf.org/下载wkhtmltopdf ,下 ...
- 议:如何将树形菜单形式的数据转化成HTML的二维表(相同内容需合并单元格)
一般做OA类管理系统,经常涉及到“组织架构”的概念,那么像这种有上下层级关系的数据一般会做成树形菜单的方式显示,底层代码必定会用到递归算法.这篇随笔的目的就是要谈谈除了用树形菜单来显示这种上下层级关系 ...
- 过滤网页中HTML代码的ASP函数
Function LoseHtml(ContentStr) Dim ClsTempLoseStr,regEx ClsTempLoseStr = Cstr(ContentStr) Set regEx = ...
- C#当中的泛型和java中的对比
1.C#中的泛型 先写一个Demo: namespace generic { public class Program { static ...
- Brackets 配置
插件 Brackets Icons 左侧导航的文件图标 FuncDocr 注释工具 QuickDocsJS js帮助文档 Beautify 格式化代码 Brackets Git git支持 ...
- iOS 9 适配需要注意的问题
iOS 9 适配需要注意的问题 1`网络适配_改用更安全的HTTPS iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用TLS 1.2 协 ...
- iOS Core Animation学习总结(3)--动画的基本类型
一. CABasicAnimation (基础动画) 移位: CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath ...
- NodeJS连接MongoDB数据库时报错
今天第一次尝试连接MongoDB数据库,具体步骤也很简单. 首先,通过NodeJS运行环境安装MongoDB包,进入要安装的目录,执行语句 npm install mongodb 安装成功后,通过如下 ...
- Java线程练习
/*线程练习创建两个线程,与主线程交替运行 */ class Text extends Thread{ private String name; Text(String name) ...