POJ3159(最短路)
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 27051 | Accepted: 7454 |
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 overc 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
题意:给出N个孩子,再给出M个限制。A,B,c表示孩子B的糖果最多比孩子A的糖果多c个。问N号孩子最多比1号孩子多多少的糖果。
思路:转化为求1号结点到N号结点的最短路问题。
/*
dijkstra Accepted 3112KB 547ms G++
*/
#include"cstdio"
#include"cstring"
#include"queue"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
struct P{
int fi,se;
P(int cfi,int cse):fi(cfi),se(cse){}
bool operator<(const P& a) const
{
return fi > a.fi;
}
};
int heap[MAXN];
int V,E;
void add_edge(int u,int v,int co)
{
es[E].to=v;
es[E].cost=co;
es[E].next=heap[u];
heap[u]=E;
E++;
}
int d[MAXN];
int dijkstra(int s)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P> que;
que.push(P(,s));
d[s]=;
while(!que.empty())
{
P p=que.top();que.pop();
int v=p.se;
if(d[v]<p.fi) continue;
for(int i=heap[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[v]+e.cost)
{ d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
return d[V];
}
int main()
{ int N,M;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(heap,-,sizeof(heap));
V=N,E=;
for(int i=;i<M;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
}
int ans=dijkstra();
printf("%d\n",ans);
} return ;
}
下面是用栈实现的spfa算法。用队列实现会TLE。
/*
spfa Accepted 3112KB 547ms G++
*/
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
int stack[MAXN],top;
int head[MAXN];
int V,E;
void add_edge(int u,int v,int co)
{
es[E].to=v;
es[E].cost=co;
es[E].next=head[u];
head[u]=E;
E++;
}
int d[MAXN];
int vis[MAXN];
int spfa(int s)
{
for(int i=;i<=V;i++) d[i]=INF;
memset(vis,,sizeof(vis));
top=;
stack[top++]=s;
vis[]=s,d[s]=; while(top!=)
{
int v=stack[--top];
vis[v]=;
for(int i=head[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
if(!vis[e.to])
{
vis[e.to]=;
stack[top++]=e.to;
}
}
}
}
return d[V];
}
int main()
{ int N,M;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(head,-,sizeof(head));
V=N,E=;
for(int i=;i<M;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
}
int ans=spfa();
printf("%d\n",ans);
} return ;
}
POJ3159(最短路)的更多相关文章
- poj3159 最短路(差分约束)
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...
- poj3159最短路spfa+邻接表
https://vjudge.net/contest/66569#problem/K 相当于模板吧,第一次写spfa的 #include<iostream> #include<cst ...
- POJ-3159 Candies 最短路应用(差分约束)
题目链接:https://cn.vjudge.net/problem/POJ-3159 题意 给出一组不等式 求第一个变量和最后一个变量可能的最大差值 数据保证有解 思路 一个不等式a-b<=c ...
- 【poj3159】 Candies
http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1 ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
随机推荐
- HTML经典标签用法
1.marquee属性的使用说明 <marquee> ... </marquee>移动属性的设置 ,这种移动不仅仅局限于文字,也可以应用于图片,表格等等 鼠标属性 onMo ...
- Linux下比较常用的svn命令
svn: command not found yum install -y subversion 以下是一些常用命令的使用方法,希望对大家有所帮助. 1,check out(co)签出代码 test. ...
- JavaScript -- JavaScript DOM 编程艺术(第2版)
/* 渐进增强 平稳退化 网页 结构层(structural layer): HTML 表示层(presentation layer): CSS <link rel="styleshe ...
- 线程池 API (转)
文档原始地址 目录 线程池概述 线程池对象 回调环境对象 工作对象 等待对象 计时器对象 I/O 完成对象 使用清理组简化清理 回调实例 API 随着 Windows Vista® 的发布 ...
- python opener代理
链接:http://www.jb51.net/article/46495.htm https://www.cnblogs.com/cunyusup/p/7341829.html
- POJ 2965:The Pilots Brothers' refrigerator
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- python 基础 7.6 sys 模块
一.sys 模块 sys 模块主要功能是获取参数 [root@www pythonscripts]# cat 2.py #!/usr/bin/python #coding=utf-8 im ...
- Hihocoder #1602 : 本质不同的回文子串的数量 manacher + BKDRhash
#1602 : 本质不同的回文子串的数量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串S,请统计S的所有子串中,有多少个本质不同的回文字符串? 注意如果 ...
- EasyNVR无插件IPC摄像机直播方案前端构建之:区分页面是自跳转还是分享依据
区分分享还是跳转 对于前端一些页面的展示,通常有两种方式:通过入口链接一步步进入,或是通过分享链接直接进入:对于这两种方式的区别是什么?在进行前端书写时又应该如何处理? 以EasyNVR为例来进行说明 ...
- SAP 已经有17个模块
SAP模块清单: 传统五大郎: MM,SD,PP,FI 财务会计CO 管理会计 +QM 质量管理 (制造业用的不少)+ABAP.BASIS.BW BI商务智能的组件之一CRM 客户管理SRM 供应商管 ...