(简单) POJ 3159 Candies,Dijkstra+差分约束。
2015-03-14 22:58
387 查看
Description
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
题目就是差分约束问题,但是这个题卡SPFA。。。。。。
代码如下:
View Code
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
题目就是差分约束问题,但是这个题卡SPFA。。。。。。
代码如下:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MaxN=30004; const int MaxM=150004; const int INF=10e9+7; struct Node { int id,val; Node(int _id=0,int _val=0):id(_id),val(_val) {} bool operator < (const Node & a) const { return val>a.val; } }; struct Edge { int to,next,cost; }; Edge E[MaxM]; int head[MaxN],Ecou; int vis[MaxN]; void Dijkstra(int lowcost[],int N,int start) { priority_queue <Node> que; Node temp; int u,v,c; for(int i=1;i<=N;++i) { lowcost[i]=INF; vis[i]=0; } que.push(Node(start,0)); lowcost[start]=0; while(!que.empty()) { temp=que.top(); que.pop(); u=temp.id; if(vis[u]) continue; vis[u]=1; for(int i=head[u];i!=-1;i=E[i].next) { v=E[i].to; c=E[i].cost; if(lowcost[v]>lowcost[u]+c && !vis[v]) { lowcost[v]=lowcost[u]+c; que.push(Node(v,lowcost[v])); } } } } void init(int N) { for(int i=1;i<=N;++i) head[i]=-1; Ecou=0; } void addEdge(int u,int v,int c) { E[Ecou].to=v; E[Ecou].cost=c; E[Ecou].next=head[u]; head[u]=Ecou++; } int ans[MaxN]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int N,M; int a,b,c; scanf("%d %d",&N,&M); init(N); while(M--) { scanf("%d %d %d",&a,&b,&c); addEdge(a,b,c); } Dijkstra(ans,N,1); printf("%d\n",ans ); return 0; }
View Code
相关文章推荐
- poj--3159--Candies(简单差分约束)
- poj--3159--Candies(简单差分约束)
- poj 3159 Candies 【简单差分约束】 【SPFA + 数组模拟栈】
- POJ 3159 Candies(差分约束,最短路)
- POJ3159_Candies_差分约束_SPFA
- poj 3159 Candies(差分约束)
- POJ 3159 Candies 还是差分约束(栈的SPFA)
- poj 3159 Candies 差分约束 + spfa
- POJ 3159 Candies(差分约束)
- POJ 3159 Candies(SPFA+栈)差分约束
- poj 3159 Candies(差分约束)
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
- POJ 题目3159 Candies(差分约束)
- 差分约束 [1](poj 3159)(简单)
- poj 3159 Candies 差分约束
- poj 3159 Candies(差分约束 spfa stack实现)
- POJ 3159 Candies 差分约束 spfa+栈+邻接表
- poj 3159 Candies(差分约束)
- POJ 3159 Candies(差分约束 转spfa+stack 求最短路径)