现在的位置: 首页 > 算法 > 正文

poj 3159

2019年02月28日 算法 ⁄ 共 2410字 ⁄ 字号 评论关闭
Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 20467   Accepted: 5421

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 throughN.
snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers AB 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

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

刚刚学SFPA,满心欢喜写好了,居然超时,后天看了discuss和网上的博客,手写了栈才过。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 1<<30
using namespace std;
struct Edge
{
    int a,b,c;
    int next;
}edge[150005];
int dist[30005];
int m,n,mm;
int fst[30005];
bool vis[30005];
int cnt[30005];
int q[90000000];
bool spfa()
{
    memset(vis,0,n+2);
    memset(cnt,0,4*(n+2));
    for(int i=1;i<=n;i++)dist[i]=maxn;
    int top=0;
    q[++top]=1;
    dist[1]=0;
    int a,b;
    vis[1] = 1 ;
    cnt[1]=1;
    while(top)
    {
        a=q[top--];
        vis[a]=0 ;
        for(int i=fst[a];i!=0;i=edge[i].next)
        {
            b=edge[i].b;
            if(dist[b]>dist[a]+edge[i].c)
            {
                dist[b]=dist[a]+edge[i].c;
                if(!vis[b])
                {
                    vis[b]=1;
                    q[++top]=b;
                    cnt[b]++;
                    if(cnt[b]>n)
                    {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
int main()
{
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        mm=0;
        memset(fst,0,4*(n+2));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            edge[++mm].next=fst[a];
            fst[a]=mm;
            edge[mm].a=a;
            edge[mm].b=b;
            edge[mm].c=c;
        }
        spfa();
        cout<<dist[n]<<endl;
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.