POJ 3159-Candies(差分約束)
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?
Input
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
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
2 2
1 2 5
2 1 4
Sample Output
5
題目大意:給n個人分糖,然後給出幾組abc,要求第a個人要求第b個人的糖不能超過自己c個。求出第n與第1個人的糖差值最大。
思路:典型的差分約束系統,不用抽象出關系式,也不用判負環,題目直接給出系統條件,然後用最短路改變一下鬆弛條件即可。
程式碼如下:
#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define swap(a,b) {int t=a;a=b;b=t}
using namespace std;
//using namespace __gnu_cxx;
#define N 200010
LL d[N];
int n,m;
int head[N];
int vis[N];
struct node{
int w;
int v;
int next;
}p[N];
void add(int u,int v,int w,int k)
{
p[k].v=v;
p[k].w=w;//權
p[k].next=head[u];//出發點
head[u]=k;
}
void spfa_stack()
{
stack<int>q;
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
d[1]=0;
vis[1]=1;
q.push(1);
while(!q.empty())
{
int u=q.top();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=p[i].next)
{
int v=p[i].v;
int w=p[i].w;
if(d[u]+w<d[v])
{
d[v]=d[u]+w;
if(vis[v]==0)
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
per(i,0,m-1)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c,i);
}
spfa_stack();
printf("%lld\n",d[n]);
}
return 0;
}
相關文章
- POJ3159 Candies【差分約束+最短路】
- 差分約束
- POJ 3169(Bellman-Ford演算法,差分約束系統)演算法
- 差分約束基本講解
- 差分約束學習筆記筆記
- 淺談差分約束系統
- 差分約束的一些理解
- [演算法學習筆記] 差分約束演算法筆記
- Atcoder ABC 216 G 01Sequence 題解 [ 藍 ] [ 差分約束 ]
- Day2 尤拉路,拓撲排序和差分約束排序
- 【SQL】15 SQL 約束(Constraints)、NOT NULL 約束、UNIQUE 約束、PRIMARY KEY 約束、FOREIGN KEY 約束、CHECK 約束、DEFAULT約束SQLAINull
- Javaweb-約束的分類JavaWeb
- 時序分析:基礎知識整理(三)差分轉單端的約束等
- 約束
- Javaweb-約束-外來鍵約束JavaWeb
- MySQL 約束MySql
- 03約束
- SQL約束SQL
- 約束CONSTRAINTAI
- 演算法隨筆——圖論之差分約束演算法圖論
- (10)邏輯綜合新增約束(環境約束)
- 主鍵約束、唯一約束和唯一索引索引
- 資料庫的集合,分頁及約束條件資料庫
- 約束介紹
- 綜合約束
- Javaweb-約束案例JavaWeb
- IDELAY約束測試IDE
- MySQL 欄位約束MySql
- MySQL自增約束MySql
- 完整性約束
- 外來鍵約束
- SQLServer約束介紹SQLServer
- 《學習》6約束
- 差分
- Day6 差分和樹上差分
- mysql資料庫約束MySql資料庫
- 約束定理+質數篩
- [20240228]約束novalidate.txt