POJ 3159 Candies(差分规划+SPFA)
来源:互联网 发布:c语言界面设计 编辑:程序博客网 时间:2024/06/07 05:30
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?
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
2 21 2 52 1 4
5
由 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
这里注意用栈代替队列,而且保存边的方式也要注意
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <iomanip>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(int i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define MOD 1000000007#define pb push_back#define mp make_pair#define fi first#define se second#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;using namespace std;typedef vector<int> vi;typedef long long ll;typedef unsigned long long ull;inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};const int N = 1e7+5;const int INF=0x3f3f3f3f;int m,n,x;const int MAXN= 30005 ;const int MAXE= 150010 ;struct Edge{ int to; int v; int next;}edge[MAXE];class SPFA{public:void init(){ tol = 0; memset(head,-1,sizeof(head));for(int i=1;i<=n;i++)dist[i]=INF;} void addedge(int a,int b,int v)//加边 { edge[tol].to=b; edge[tol].v=v; edge[tol].next=head[a]; head[a]=tol++; }bool Run(int start,int n){vis[start]=true;dist[start]=0; int top=0; int ss[30010];ss[top++]= start;while(top!=0){int u=ss[--top];vis[u]=false; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(dist[v]>dist[u]+edge[i].v) { dist[v]=dist[u]+edge[i].v; if(!vis[v]) { vis[v]=true; ss[top++]=v; } } }}return true;}public:int dist[MAXN];private: int tol; int head[MAXN];//每个结点的头指针bool vis[MAXN];//在队列标志};int main(){ ios::sync_with_stdio(false); int u; int v; int w; SPFA sf; scanf("%d %d",&n,&m); sf.init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); sf.addedge(u,v,w); } sf.Run(1,n); printf("%d\n",sf.dist[n]); return 0;}
- POJ 3159 Candies(差分规划+SPFA)
- POJ 3159 Candies(差分规划+SPFA)
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- POJ 3159 Candies【差分约束+SPFA】
- poj 3159 Candies(差分约束 spfa stack实现)
- POJ 3159 Candies(SPFA+栈)差分约束
- POJ 3159 Candies(spfa最短路,差分约束)
- POJ 3159 Candies(差分约束+SPFA )
- 【poj 3159】Candies 差分约束+spfa(stack)
- 【POJ 3159】Candies(差分约束+SPFA)
- POJ 3159--Candies【基础差分规划】
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- POJ 3159 Candies 差分约束 spfa+栈+邻接表
- POJ 3159 Candies(差分约束基础题,栈式SPFA)
- POJ 3159 Candies 差分约束-SPFA栈实现
- Candies+差分约束+spfa+poj
- poj Candies(差分约束,spfa栈)
- 《产品经理方法论》
- STM32笔记0428/建立模板
- 关于Android源码的获取
- 4.29面试总结
- LeetCode 130. Surrounded Regions (并查集)
- POJ 3159 Candies(差分规划+SPFA)
- openvpn通过ldap或ad统一认证解决方案思路分享
- 学习淘淘商城第三十七课(中文分析器IK-Analyzer的使用)
- 第七章 类
- mysql--查看版本
- Effective Java系列读后感(三)-类和接口
- C++ 文件操作
- eclipse 常用快捷键整理
- WiFi、ZigBee、BLE用哪个?小米内部是这样选的