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的更多相关文章

  1. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  2. (poj)3159 Candies

    题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...

  3. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  4. 线段树(单点更新) POJ 2886 Who Gets the Most Candies?

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  5. POJ——T 3159 Candies

    http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   ...

  6. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  7. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  8. POJ 2886Who Gets the Most Candies?(线段树)

    POJ 2886 题目大意是说有n个人围成一圈,游戏的起点是k,每个人持有一个数字(非编号)num,每次当前的人退出圈,下一个人是他左边的第num个(也就是说下一个退出的是k+num, k可以为负数, ...

  9. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

随机推荐

  1. oracle 零散知识汇集

    1. Select '登陆' + 2 From dual会报错: ora- 01722 无效数字,原理是oracle把'登陆'当成数字来和2进行加法运算. Select '登陆'|| 2 From d ...

  2. Demon_背包系统(实现装备栏,背包栏,可以切换装备)

    using UnityEngine; using System.Collections; public enum BoxType { Normal,//普通格子 Equip//装备栏格子 } publ ...

  3. Libgdx New 3D API 教程之 -- Libgdx中使用Materials

    This blog is a chinese version of xoppa's Libgdx new 3D api tutorial. For English version, please re ...

  4. 统计学习导论:基于R应用——第五章习题

    第五章习题 1. 我们主要用到下面三个公式: 根据上述公式,我们将式子化简为 对求导即可得到得到公式5-6. 2. (a) 1 - 1/n (b) 自助法是有有放回的,所以第二个的概率还是1 - 1/ ...

  5. [ArcGIS所需的补丁]ArcGIS 10.2.2 for Desktop联系Oracle(2014年10上个月发布)数据库崩溃

    环境的叙述性说明: ArcGIS 10.2.2 for Desktop.Oracle12.1.0.2 RAC 这仅仅是用户当时环境描写叙述.可是导致该问题的解决办法还包含很多其它的环境! 问题描写叙述 ...

  6. [转] __thread关键字

    http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量 ...

  7. 《Android开发艺术探索》读书笔记 (6) 第6章 Android的Drawable

    本节和<Android群英传>中的第六章Android绘图机制与处理技巧有关系,建议先阅读该章的总结 第6章 Android的Drawable 6.1 Drawable简介 (1)Andr ...

  8. android ImageView scaleType属性(转)

    使用ImageView时经常会用到scaleType属性,如: 1 2 3 4 5 6 7 8 9 <ImageView   android:layout_width="50dp&qu ...

  9. Upgrade to Python 2.7.9 on CentOS5.5

    1. Install python2.7 #cd /tmp #wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz --no-ch ...

  10. 自定义android精美聊天界面

    编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...