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] ...
随机推荐
- dependencyManagement与dependencies区别
最近在阅读maven项目代码时,dependencyManagement与dependencies之间的区别不是很了解,现通过项目实例进行总结:项目epps-demob-pom下有一个模块是epps- ...
- 查看Oracle最耗时的SQL
有很多种方法可以用来找出哪些SQL语句需要优化,但是很久以来,最简单的方法都是分析保存在V$SQL视图中的缓存的SQL信息.通过V$SQL视图,可以确定具有高消耗时间.CUP和IO读取的SQL语句. ...
- 《Windows编程零基础学》第零节
首先很开心申请到了这一个专栏<Windows编程零基础学> 这是第一篇文章,在这里,我将讲述一些基础的知识. 什么是Windows编程 所谓Windows编程就是在Windows平台上开发 ...
- 操作SharedPreferences的注意点
如果使用SharedPreferences用于数据存取,大部分人喜欢使用如下代码: public void writeSharedprefs(int pos) { SharedPreferences ...
- 滑动到底部或顶部响应的ScrollView实现
关于使用可见:滚动到底部或顶部响应的ScrollView使用 示例APK可从这些地址下载:Google Play, 360手机助手, 百度手机助手, 小米应用商店, 豌豆荚 两种实现方式的主要 ...
- static作用——The static effect
1)在函数体内,一个被声明为静态的变量在这一函数被调用过程中维持其值不变(该变量存放在静态变量区). 2) 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它 ...
- Linux基本配置和管理 2 ---- Linux多命令协作----管道及重定向
1 管道和重定向 1 在Linux中大多数命令都很简单,很少出现复杂的命令,每个命令只是实现一个简单的功能,我们可以通过组合不同的命令来实现复杂的功能 2 在Linux中几乎所有的命令返回的数据都是纯 ...
- Queueing in the Linux Network Stack !!!!!!!!!!!!!!!
https://www.coverfire.com/articles/queueing-in-the-linux-network-stack/ Queueing in the Linux Networ ...
- [转] nginx+FastCGI+c++
from: http://www.cnblogs.com/xiaouisme/archive/2012/08/01/2618398.html 一 安装 目的:不需支持php等.就html就行了.步骤: ...
- (转)mysql 的 find_in_set函数使用方法
举个例子来说: 有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1 ...