您的位置:首页 > 其它

poj 3159 Candies (spfa+stack)

2016-03-18 20:06 423 查看
http://poj.org/problem?id=3159

Candies

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 27564 Accepted: 7593
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
2 2
1 2 5
2 1 4

Sample Output
5


题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果

注意:数据较大,用邻接表存储,还有就是用stack600多MS过,用queue的话就TLE了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <stack>

using namespace std;

#define N 200000
#define INF 0x3f3f3f3f
#define met(a, b) memset (a, b, sizeof (a))

int vis
, dist
, head
, cnt, n, m;

struct node
{
int u, v, flow, next;
} edge[N*4];

void Init ()
{
met (vis, 0);
met (head, -1);

for (int i=0; i<=n; i++)
dist[i] = INF;

cnt = 0;
}

void addedge (int u, int v, int flow)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].flow = flow;
edge[cnt].next = head[u];
head[u] = cnt++;
}

void spfa ()
{
stack <int> sta;

while (!sta.empty()) sta.pop();

sta.push (1);
dist[1] = 0;
vis[1] = 1;

while (!sta.empty())
{
int u = sta.top(); sta.pop();
vis[u] = 0;

for (int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].v;
int flow = edge[i].flow;

if (dist[v] > dist[u] + flow)
{
dist[v] = dist[u] + flow;
if (!vis[v])
{
sta.push (v);
vis[v] = 1;
}
}
}
}
printf ("%d\n", dist
);
}

int main ()
{
while (scanf ("%d %d", &n, &m) != EOF)
{
Init();
int u, v, flow;
for (int i=1; i<=m; i++)
{
scanf ("%d %d %d", &u, &v, &flow);
addedge (u, v, flow);
}
spfa ();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: