poj3159 最短路(差分约束)

题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个。

这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 a 多 c 个,也就是 d[b] - d[a] ≤ c,就可以建立 value 值为 c 的单向边 e(a,b) ,然后先定d[1] = 0 ,用最短路跑完得到的 d[n] 就是所求答案。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<vector>
 6 using namespace std;
 7 typedef pair<int,int> pii;
 8
 9 int head[30005],point[150005],val[150005],next[150005],size;
10 int dist[30005];
11
12 struct cmp{
13     bool operator()(pii a,pii b){
14         return a.first>b.first;
15     }
16 };
17
18 void add(int a,int b,int v){
19     point[size]=b;
20     val[size]=v;
21     next[size]=head[a];
22     head[a]=size++;
23 }
24
25 void dij(int s,int t){
26     int i;
27     priority_queue<pii,vector<pii>,cmp>q;
28     memset(dist,0x3f,sizeof(dist));
29     dist[s]=0;
30     q.push(make_pair(dist[s],s));
31     while(!q.empty()){
32         pii u=q.top();
33         q.pop();
34         if(u.first>dist[u.second])continue;
35         for(i=head[u.second];~i;i=next[i]){
36             int j=point[i];
37             if(dist[j]>dist[u.second]+val[i]){
38                 dist[j]=dist[u.second]+val[i];
39                 q.push(make_pair(dist[j],j));
40             }
41         }
42     }
43     printf("%d\n",dist[t]);
44 }
45
46 int main(){
47     int n,m;
48     while(scanf("%d%d",&n,&m)!=EOF){
49         int i;
50         memset(head,-1,sizeof(head));
51         size=0;
52         for(i=1;i<=m;i++){
53             int a,b,v;
54             scanf("%d%d%d",&a,&b,&v);
55             add(a,b,v);
56         }
57         dij(1,n);
58     }
59     return 0;
60 }

时间: 2024-08-02 12:13:49

poj3159 最短路(差分约束)的相关文章

poj3159——Candies(差分约束+SPFA堆栈)

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 ofte

K - Candies(最短路+差分约束)

题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面是查分约束详解: 一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题 好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面开始详细介绍下 比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<

POJ3169(最短路+差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7613   Accepted: 3658 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

poj3159 Candies(差分约束,dij+heap)

poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d[v] – d[u] <= w. 再看题目给出的关系:b比a多的糖果数目不超过c个,即d[b] – d[a] <= c ,正好与上面模型一样, 所以连边a->b,最后用dij+heap求最短路就行啦. ps:我用vector一直TLE,后来改用前向星才过了orz... 1 #include&

poj3169 最短路(差分约束)

题意:一个农夫有n头牛,他希望将这些牛按照编号 1-n排成一条直线,允许有几头牛站在同一点,但是必须按照顺序,有一些牛关系比较好,希望站的距离不超过某个值,而有一些牛关系不太好,所以希望站的距离大于等于某个值,问1号牛和n号牛之间的最远距离是多少. 差分约束的裸题,对于 d[v] - d[u] ≤ w 建立权值为 w 的单向边 e(u,v),对于 d[v] - d[u]  ≥ w 建立权值为 -w 的单向边 e(v,u),然后再根据牛必须按顺序排列建立权值为 0 的边 e(i+1,i),然后最短

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;

训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - BellmanFord - 图论 - 训练指南 - 差分约束 Halum UVA - 11478 题意 带权有向图,每个点都可以有如下操作:令从ta出发的每一条边增加d,终止于ta的每一条边减小d 最后让所有边权的最小值非负且尽量大 题

UVA 11478 - Halum(差分约束+最短路)

UVA 11478 - Halum 题目链接 题意:给定一个有向图,每次操作可以选择一个结点,把以这个点为起点的边权值+d,以这个边为终点的-d,问经过操作后,能得到的边权最小的最大值是多少,并且要判但是否无穷大或无解 思路:转化为差分约束,设一条边,他增加的权值为sum(u)减少了sum(v),那么二分答案x,得到一个不等式sum(u) - sum(v) + w(u, v) >= x,变形后得到sum(v) - sum(u) <= w(u, v) - x,这样就转化为了差分约束,直接bell

【POJ3169 】Layout (认真的做差分约束)

Layout Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they ar