poj Candies
http://poj.org/problem?id=3159
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#define max1 30005
#define max2 150005
using namespace std;
int head[max1],next[max2];
int dis[max1];
bool vis[max1];
const int inf=<<;
struct node
{
int u,v;
int c;
node(){}
node(int u,int v,int c):u(u),v(v),c(c){}
}p[max2]; 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;
}
};
int e,n,m;
void addnode(int u,int v,int c)
{
p[e]=node(u,v,c);
next[e]=head[u];
head[u]=e++;
} bool relax(int u,int v,int c)
{
if(dis[v]>dis[u]+c)
{
dis[v]=dis[u]+c;
return true;
}
return false;
} void inti()
{
memset(head,-,sizeof(head));
memset(next,-,sizeof(next));
e=;
for(int i=; i<m; i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
addnode(u,v,c);
}
} void dij(int src)
{
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=inf;
dis[src]=;
priority_queue<node1>que;
que.push(node1(src,dis[src]));
for(int i=; i<n; i++)
{
while(!que.empty()&&vis[que.top().v])
que.pop();
if(que.empty()) break;
node1 pre=que.top(); que.pop();
vis[pre.v]=true;
for(int j=head[pre.v]; j+; j=next[j])
{
if(relax(pre.v,p[j].v,p[j].c)&&!vis[p[j].v])
que.push(node1(p[j].v,dis[p[j].v]));
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
inti();
dij();
printf("%d\n",dis[n]);
}
return ;
}
poj Candies的更多相关文章
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- (poj)3159 Candies
题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 线段树(单点更新) POJ 2886 Who Gets the Most Candies?
题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...
- POJ——T 3159 Candies
http://poj.org/problem?id=3159 Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 33328 ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ 2886Who Gets the Most Candies?(线段树)
POJ 2886 题目大意是说有n个人围成一圈,游戏的起点是k,每个人持有一个数字(非编号)num,每次当前的人退出圈,下一个人是他左边的第num个(也就是说下一个退出的是k+num, k可以为负数, ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
随机推荐
- 【Cocos2d-X开发学习笔记】第29期:游戏中数据的存储(上)
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一般游戏中都需要记录玩家数据,便于玩家下次登录时 ...
- Oracle内存结构(之三)
[深入解析--eygle] 学习笔记 1.2.3 PGA的管理: sys@felix SQL>show parameter area_size NAME TYPE VALUE --------- ...
- C++中模板类使用友元模板函数
在类模板中可以出现三种友元声明:(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数.(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权.(3)只授予对类模板或函数模板的特定 ...
- Quarts SimpleTrigger going to BLOCKED state after few repeat intervals--stackoverflow
question: I am using SimpleTrigger to schedule a job which is supposed to run indefinitely (repeat c ...
- 详解Android Handler的使用-别说你不懂handler(转)
我们进行Android开发时,Handler可以说是使用非常频繁的一个概念,它的用处不言而喻.本文就详细介绍Handler的基本概念和用法. Handler的基本概念 Handler主 ...
- 成为VR开发者的六个基本问题
2016-05-31 小小CTO 未来CTO 我常被问起能否提供更多的建议,帮助其他人进入这个行业,或是做VR开发人员该怎么赚钱.我很难具体回答,因为每个人的情况都很不一样.不过还是有一些建议适用于想 ...
- vim 设定一个新的snippets
目录 ~/.vim/bundle/vim-snippets/snippets 编辑 _.snippets 在最后添加一行自己需要的,比如想要个自己的名字,然后按tab,即出现全称,可以添加如下内容 s ...
- ansible小结
一.Ansible的安装 1.yum源安装 以centos为例,默认在源里没有ansible,不过在fedora epel源里有ansible,配置完epel 源后,可以直接通过yum 进行安装.这里 ...
- Linux开发工具之gdb(上)
三.gdb调试(上) 01.gdb:gdb是GNU debugger的缩写,是编程调试工作. 功能: 启动程序,可以按照用户自定义的要求随心所欲的运行程序: 可让被调试的程序在用户所指定的调试 ...
- centos 给终端设快捷键
centos 终端的快捷键是默认是禁用的 设置的话 系统-> 首选项 -> 键盘快捷键 看到运行终端 随便设置想要的快捷键!!