链接:https://vjudge.net/problem/POJ-3159

题意:

N个小孩,M个约束

以A,B,C给出。即小孩B的糖果不能比A多C以上。

思路:

差分约束:

若有 A-B <= V1,B-C <= V2,即A-C <= V1+V2。

用最短路求法求。

A->B = V1,B->C=V2,A->C=V3,则有A->C = min(A->C,(A->B+B->C))。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
const int MAXN = 150000+10;
const long long INF = 99999999;
typedef long long LL;
//邻接表存图优化Dijkstra算法
struct Node
{
int _w;
long long _v;
bool operator < (const Node & that)const
{
return this->_v > that._v;
}
Node(int w,int v):_w(w),_v(v){}
};
int First[MAXN],Next[MAXN];
int u[MAXN],v[MAXN],w[MAXN]; int Dis[MAXN];
int Vis[MAXN];
int n,m; void Read_Graph()
{
scanf("%d%d",&n,&m);
for (int e = 1;e <= m;e++)
{
scanf("%d%d%d",&u[e],&v[e],&w[e]);
}
} void Init()
{
for (int i = 1;i<=m;i++)
First[i] = -1;
for (int e = 1;e <= m;e++)
{
Next[e] = First[u[e]];
First[u[e]] = e;
}
} int Dijkstra()
{
for (int i = 1;i <= n;i++)
{
Dis[i] = INF;
Vis[i] = 0;
}
Dis[1] = 0;
priority_queue<Node> que;
que.push(Node(1,0));
while (!que.empty())
{
if (Vis[que.top()._w] == 1)
que.pop();
else
{
int e = que.top()._w;
Vis[e] = 1;
e = First[e];
while (e != -1)
{
if (Dis[v[e]] > Dis[u[e]] + w[e])
{
Dis[v[e]] = Dis[u[e]] + w[e];
que.push(Node(v[e],Dis[v[e]]));
}
e = Next[e];
}
que.pop();
}
}
return Dis[n];
} int main()
{
Read_Graph();
Init();
cout << Dijkstra() << endl; return 0;
}

  

最新文章

  1. 【转】浅谈JavaScript、ES5、ES6
  2. qt 环境下mapx组件打包后编译产生c2248和c2512错误
  3. linux基础-第十单元 系统的初始化和服务
  4. 奇怪吸引子---TreeScrollUnifiedChaoticSystem
  5. android开源框架
  6. 【原创】牛顿法和拟牛顿法 -- BFGS, L-BFGS, OWL-QN
  7. [IDL入门] 两个PPT,IDL上手
  8. DELL EqualLogic PS存储硬盘故障数据恢复成功案例分享
  9. NavigationView头部设置监听事件
  10. 论文笔记:Decoders Matter for Semantic Segmentation: Data-Dependent Decoding Enables Flexible Feature Aggregation
  11. 利用Oracle分析函数row_number和sys_connect_by_path实现多行数据合并为一行
  12. Python之网络编程(Socket)
  13. jQuery Autocomplete 备忘录
  14. 解决:CentOS下的 error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or dir
  15. 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr
  16. ajax请求工具类
  17. 计算器软件实现系列(七)WPF+SQL+策略模式
  18. linux===linux后台运行和关闭、查看后台任务(转)
  19. ArcGIS_Server的安装
  20. case编写的httpd简单启停脚本

热门文章

  1. Java 出现“Illegal key size”错误的解决方案
  2. 矩阵管理——和visitor模式没有本质区别,都是为了避免资源重复
  3. css 浏览器兼容性问题集合
  4. Android Dalvik虚拟机
  5. Logcat不显示Application的解决办法
  6. [Balkan 2007] Mokia
  7. the generation has been cancelled because errors have been found by the check model
  8. groovy语言和grails框架
  9. R文件报错
  10. bzoj3676