POJ-3159-Candies(SPFA+模拟栈)
2014-04-14 17:32
267 查看
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 over c candies
more than he did.
Output
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
Sample Output
思路:用SPFA+模拟队列超时了,改成栈就AC了。。。
#include <cstdio>
#include <stack>
#define INF 99999999
using namespace std;
struct E{
int v,w;
}e[150000];
stack<int>s;
int d[30001],first[30001],next[150000];
bool ins[30001];
int main()
{
int n,m,i,u,v,w;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) ins[i]=0,d[i]=INF,first[i]=-1;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
e[i].v=v;
e[i].w=w;
next[i]=first[u];
first[u]=i;
}
s.push(1);
d[1]=0;
while(!s.empty())
{
u=s.top();
s.pop();
ins[u]=0;
for(i=first[u];i>=0;i=next[i])
{
if(d[e[i].v]>d[u]+e[i].w)
{
d[e[i].v]=d[u]+e[i].w;
if(!ins[e[i].v])
{
s.push(e[i].v);
ins[e[i].v]=1;
}
}
}
}
printf("%d\n",d
);
}
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 over c 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
思路:用SPFA+模拟队列超时了,改成栈就AC了。。。
#include <cstdio>
#include <stack>
#define INF 99999999
using namespace std;
struct E{
int v,w;
}e[150000];
stack<int>s;
int d[30001],first[30001],next[150000];
bool ins[30001];
int main()
{
int n,m,i,u,v,w;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) ins[i]=0,d[i]=INF,first[i]=-1;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
e[i].v=v;
e[i].w=w;
next[i]=first[u];
first[u]=i;
}
s.push(1);
d[1]=0;
while(!s.empty())
{
u=s.top();
s.pop();
ins[u]=0;
for(i=first[u];i>=0;i=next[i])
{
if(d[e[i].v]>d[u]+e[i].w)
{
d[e[i].v]=d[u]+e[i].w;
if(!ins[e[i].v])
{
s.push(e[i].v);
ins[e[i].v]=1;
}
}
}
}
printf("%d\n",d
);
}
相关文章推荐
- Android4 学习笔记 4-视频技术概述
- 使用 XML 查询替换 ADO.NET 中的 IN ,提高查询性能
- 电容的原理与应用(补充中)
- poj1276
- 图论的基础知识点
- ZOJ 3780 Paint the Grid Again
- freebsd8.1+samba 的配置文件for太阳鸟
- String string = ""; 与String string = new String();的区别
- jaas之登录实例
- warning C4305: “=”: 从“int”到“unsigned char”截断解决方法[zz]
- 【DOM编程艺术】document对象的write方法
- php 5.3.3以后的版本中 php-fpm 的重启、终止操作命令
- Codeforces Round #150 (Div. 2) C. The Brand New Function
- protobuf相关:反射、rmi
- 最大值和最小值
- BS与CS的联系与区别
- 输出链表倒数第k个元素
- 关于C++ map容器迭代器越界的研究
- david's samba config files
- 第十二届北京师范大学程序设计竞赛热身赛第一场 C. Adidas vs Adivon