链接: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;
}

  

POJ-3159-Candies-(差分约束,Dijkstra)的更多相关文章

  1. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  2. POJ 3159 Candies 差分约束dij

    分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...

  3. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  4. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  5. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  6. (简单) POJ 3159 Candies,Dijkstra+差分约束。

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

  7. POJ 3159 Candies 【差分约束+Dijkstra】

    <题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...

  8. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  9. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  10. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

随机推荐

  1. UEFI启动模式下安装Ubuntu 16.04教程【转】

    本文转载自:http://blog.csdn.net/Jesse_Mx/article/details/61425361 前言 最近常帮人安装Ubuntu,也算积累了一些经验.这篇博文主要谈一谈如何在 ...

  2. WebDriver API——延时操作及元素等待

    在自动化测试过程当中,受网络.测试设备等诸多因素的影响,我们经常需要在自动化测试脚本中添加一些延时来更好的定位元素来进行一系列的操作. 一般有这么几种方式: 1.implicitlyWait.识别对象 ...

  3. POJ 2970 The lazy programmer(贪心+单调优先队列)

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...

  4. JAVA解析EXCEL(2003和2007)

    本文参考: http://wenku.baidu.com/view/707f07d95022aaea998f0fd1.html http://surfingforrest.iteye.com/blog ...

  5. windows兼容dirent.h

    尝试在windows下跑KCF算法,创建工程编译后出现: Error 4 error C1083: Cannot open include file: 'dirent.h': No such file ...

  6. ubuntu解决挂起后不能唤醒

    安装 laptop-mode 如果你不缺认自已是否安装了laptop-mode-tools工具包,可以在终端中输入下列命令来确认是否安装. dpkg -l | grep laptop-mode-too ...

  7. Flutter实战视频-移动电商-37.路由_Fluro引入和商品详细页建立

    37.路由_Fluro引入和商品详细页建立 https://github.com/theyakka/fluro pages/details_page.dart新建页面 使用路由 先添加路由插件的引用 ...

  8. 14.oauth2与open id connect 对比

    微博的授权机制 openIdConnect

  9. 7.11实习培训日志-Git Linux

    Git git子模块 先在GitHub创建两个空的respository,一个super_project和一个sub_project. 然后在git bash中向库中写入一些文件. 在super_pr ...

  10. springboot2 -广播式WebSocket

    1.WebSocket,STOMP,SockJS含义 WebSocket:WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. SockJS:SockJS 是 We ...