POJ3159 Candies【差分約束+最短路】
Candies
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 38621 | Accepted: 10891 |
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?
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
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly--2006.12.31, Sempr
問題連結:POJ3159 Candies
問題描述:n個人,m個資訊,每行的資訊是3個數字,A,B,C,表示B比A多出來的糖果不超過C個,問你,n號人最多比1號人多幾個糖果。
解題思路:差分約束+Dijkstra+堆優化+前向星
AC的C++程式:
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N=30005;
const int INF=0x3f3f3f3f;
int dist[N];
bool vis[N];
//前向星實現圖的鄰接表
int M;
int head[N];
struct Edge{
int to,next,w;
}g[160000];
void add(int from,int to,int w)
{
g[M].next=head[from],g[M].to=to,g[M].w=w;
head[from]=M++;
}
struct Node{
int u,w;
Node(){}
Node(int u,int w):u(u),w(w){}
bool operator<(const Node &a)const
{
return w>a.w;
}
};
void dijkstra(int s)//s表示起點
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
priority_queue<Node>q;
q.push(Node(s,0));
dist[s]=0;
while(!q.empty()){
Node f=q.top();
q.pop();
int u=f.u;
if(!vis[u]){
vis[u]=true;
for(int i=head[u];i!=-1;i=g[i].next){
int v=g[i].to;
if(!vis[v]){
if(dist[v]>dist[u]+g[i].w){
dist[v]=dist[u]+g[i].w;
q.push(Node(v,dist[v]));
}
}
}
}
}
}
int main()
{
int n,m,a,b,c;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
M=0;
while(m--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
dijkstra(1);//計算1號站到其他站的最短路徑
printf("%d\n",dist[n]);
return 0;
}
相關文章
- POJ 3159-Candies(差分約束)
- 差分約束
- 差分約束基本講解
- 差分約束學習筆記筆記
- 淺談差分約束系統
- 差分約束的一些理解
- [演算法學習筆記] 差分約束演算法筆記
- Atcoder ABC 216 G 01Sequence 題解 [ 藍 ] [ 差分約束 ]
- Day2 尤拉路,拓撲排序和差分約束排序
- POJ 3169(Bellman-Ford演算法,差分約束系統)演算法
- 【SQL】15 SQL 約束(Constraints)、NOT NULL 約束、UNIQUE 約束、PRIMARY KEY 約束、FOREIGN KEY 約束、CHECK 約束、DEFAULT約束SQLAINull
- Javaweb-約束的分類JavaWeb
- 時序分析:基礎知識整理(三)差分轉單端的約束等
- 最短路:求最長最短路
- 最短路 || 最長路 || 次短路
- 約束
- Javaweb-約束-外來鍵約束JavaWeb
- CF79D Password (差分+狀壓 dp+最短路/bfs)
- 第一週【任務2】無約束最優化優化
- MySQL 約束MySql
- 03約束
- SQL約束SQL
- 約束CONSTRAINTAI
- 演算法隨筆——圖論之差分約束演算法圖論
- (10)邏輯綜合新增約束(環境約束)
- 主鍵約束、唯一約束和唯一索引索引
- 資料庫的集合,分頁及約束條件資料庫
- 約束介紹
- 綜合約束
- 分層圖最短路
- Javaweb-約束案例JavaWeb
- IDELAY約束測試IDE
- MySQL 欄位約束MySql
- MySQL自增約束MySql
- 完整性約束
- 外來鍵約束
- SQLServer約束介紹SQLServer
- 《學習》6約束