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