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] ...
随机推荐
- ios-简单算法
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- Visual Studio的2个有趣的插件:声音控制和放屁:)
.NET Slave | Talk to, hear, touch and see your code介绍了2个有趣的Visual Studio的插件,你可以通过它们和你的代码对话. 声音控制(Voi ...
- OSChina 其中很重要的一类——RequestContext
RequestContext 这个类在 OSChina 中是很重要的一个类.该类由全局 Filter 进行初始化.并传递给包含 Action 和 页面中直接使用.使用时通过 RequestContex ...
- MYSQL 体系结构图 log commit
- LeetCode:Permutations(求全排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- java内存不足
-Xmx1024m -Xms1024m -XX:PermSize=128m -XX:MaxPermSize=512m ------------------------- 亲测可用
- [转] linux中cat more less head tail 命令
1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具.查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名. 比如: [root@localhost ~]# c ...
- js页面加载事件
<body onload="myfunction()" > </body> <script type="text/javascript&qu ...
- windows服务安装(System.ComponentModel.Win32Exception:远程过程调用失败)
“安装”阶段发生异常.System.ComponentModel.Win32Exception:远程过程调用失败 附上提示信息C:\Windows\Microsoft.NET\Framework\v4 ...
- 重装系统时,将MBR分区转为GPT 分区
摘要 很多同学在重装系统的时候,或多或少都遇到过这样的问题:镜像文件没有问题,软碟通刻录也没有问题,但偏偏就在选择安装系统盘盘符的时候,跳出对话框,提示:Windows无法安装到这个磁盘,选中的磁盘具 ...