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 ...
随机推荐
- 基于友善之臂ARM-tiny4412--uboot源代码分析
/* * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core * * Copyright (c) 2004 Texas Instrument ...
- js时间戳格式化成日期格式的多种方法
js需要把时间戳转为为普通格式,一般的情况下可能用不到的, 下面先来看第一种吧 复制代码代码如下: function getLocalTime(nS) { return new Date(parseI ...
- java 常用设计模式(转载)
http://www.cnblogs.com/hnrainll/archive/2011/12/29/2305582.html 设计模式:一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得 ...
- ant 可自动替换友盟渠道、版本号、包名
可自动替换友盟渠道.版本号.包名 如何集成到我的项目里 前提:了解android官方文档,在项目目录中执行ant debug能打包,比如常见的打包步骤: android update project ...
- 资源:Localization – 本地化
Resource Dictionary –资源字典 所有的资源项在最终都会被整合到Resource Dictionary中的,也就是说无论是FrameworkElement的Resources,还是W ...
- React中key的必要性与使用
React这个框架的核心思想是,将页面分割成一个个组件,一个组件还可能嵌套更小的组件,每个组件有自己的数据(属性/状态);当某个组件的数据发生变化时,更新该组件部分的视图.更新的过程是由数据驱动的,新 ...
- python 基础 5.2 类的继承
一. 类的继承 继承,顾名思议就知道是它的意思,举个例子说明,你现在有一个现有的A类,现在需要写一个B类,但是B类是A类的特殊版,我们就可以使用继承,B类继承A类时,B类会自动获得A类的所有属性和方法 ...
- consistence availability partition tolerance quit
理论证明
- 怎样做大做强企业中的ERP?
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luozhonghua2014/article/details/37672409 ...
- Android 修改Menu字体颜色和背景
我们知道,在Android中修改TextView的字体颜色,一般是通过setTextColor()方法.虽说Android的Menu菜单项的每一项都是由TextView组成,但是Android的sdk ...