poj3159 Candies
来源:互联网 发布:c语言获取系统时间 编辑:程序博客网 时间:2024/05/17 07:30
题意:n个学生分糖果,有一组限制,如a认为b得到的糖果不多于自己k个,即b-a<=k,求解第n个同学与第一个同学最多相差多少个糖果。
分析:差分约束系统,求最大相差,可设d[1]=0,求最短路径,d[n]即为答案。(可参考另一篇博客《差分约束系统》)。Dij加heap就可以过了,据说队列spfa会超时,用spfa+
栈可过。
#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>#include<map>#include<algorithm>#include<queue>#include<set>#define inf 10000000#define pi acos(-1.0)#define eps 1e-8#define seed 131using namespace std;typedef pair<int,int> pii;typedef unsigned long long ULL;typedef long long LL;const int maxn=30005;int n,m;struct Edge{ int from,to,w; Edge(){} Edge(int a,int b,int c):from(a),to(b),w(c){}}edge[maxn*5];int e=0;int head[maxn];int next[maxn*5];int d[maxn];void Dij();int main(){ scanf("%d%d",&n,&m); memset(head,-1,sizeof(head)); int a,b,c; for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); edge[e].from=a; edge[e].to=b; edge[e].w=c; next[e]=head[a]; head[a]=e; e++; } Dij(); cout<<d[n]<<endl; return 0;}void Dij(){ for(int i=1;i<=n;i++) d[i]=inf; bool vis[maxn]; memset(vis,0,sizeof(vis)); d[1]=0; priority_queue< pii,vector<pii>,greater<pii> >que; que.push(pii(0,1)); while(!que.empty()) { pii u=que.top(); que.pop(); if(vis[u.second]) continue; vis[u.second]=1; for(int i=head[u.second];i!=-1;i=next[i]) { Edge& t=edge[i]; if(!vis[t.to]&&d[t.to]>u.first+t.w) { d[t.to]=u.first+t.w; que.push(pii(d[t.to],t.to)); } } }}
0 0
- poj3159 Candies
- poj3159 Candies
- poj3159 Candies
- 【poj3159】 Candies
- [POJ3159]candies
- poj3159——Candies
- POJ3159——Candies
- poj3159 Candies(SPFA+stack)
- poj3159 candies dijkstra
- POJ3159-Candies-最短路
- poj3159--Candies(差分约束)
- Candies-POJ3159差分约束
- 差分约束 poj3159 Candies
- 【差分约束系统】Candies POJ3159
- 【POJ3159】Candies 裸的pqspfa模版题
- poj3159 Candies--单源最短路径&差分约束
- poj3159 Candies(差分约束入门)
- poj3159-Candies差分约束-spfa+栈
- 会话cookie中缺少secure属性
- C#3.0 为我们带来什么(1) —— LINQ之Lambda
- 学习2
- 柯文哲《生死的智慧》
- 求两个用链表表示的数字和
- poj3159 Candies
- 有关Android中的java.lang.ClassCastException错误
- Vs2013 类派生+默认值+参数数组,默认值设置不当导致IDE奔溃或编译错误:-1073740791
- linux top详解
- vpn
- windows下使用VS2010编译jpeglib(实际操作vs2008也可以)
- C#网络编程,多个客户端连接服务器端并发送消息
- 指针函数与函数指针的区别
- URAL 2013 Neither shaken nor stirred dfs 模拟