【spfa(栈)||堆优dijkstra】poj3159

Candies

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
  
差分约束,虽然还没看到那儿,但是高中讲过毕竟有点点印象,所以还是构造出来了
但是用spfa会超时(priority_queue和手工循环队列都会tle)然后看了poj的discuss说是可以用栈……这个道理都差不多但是确实是想不到这么干
当然还可以用堆优的dijkstra
  
spfa(栈):
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;;
const long N =  30010, M = 150010, Q = N * 2;
const long INF = 0x3f3f3f3f;
struct ty
{
    long t, w, next;
};
long n, t, m;
long head[N];
ty edge[M];
long q[Q];
bool v[N];
long dist[N];
void insertedge(long x, long y, long z, long k)
{
    edge[k].t = y;
    edge[k].w = z;
    edge[k].next = head[x];
    head[x] = k;
}
void init()
{
    memset(head, 0, sizeof(head));
    memset(edge, 0, sizeof(edge));
    m = 0;
    for (long i = 1; i <= t; i++)
    {
        long x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        insertedge(x, y, z, ++m);
    }
}
void spfa()
{
    memset(v, 0, sizeof(v));
    memset(dist, 127, sizeof(dist));
    long  r = 0;
    q[++r] = 1;
    v[1] = true;
    dist[1] = 0;
    while (r)
    {
        long x = q[r--];
        v[x] = false;
        for (long i = head[x]; i != 0; i = edge[i].next)
        {
            long t = edge[i].t;
            if (dist[t] > dist[x] + edge[i].w)
            {
                dist[t] = dist[x] + edge[i].w;
                if (!v[t])
                {
                    q[++r] = t;
                    v[t] = true;
                }
            }
        }
    }
    cout << dist[n] << endl;
}
int main()
{
    while(scanf("%d%d", &n, &t) != EOF)
    {
        init();
        spfa();
    }
    return 0 ;
}


堆优dijkstra:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;;
const long N =  30010, M = 150010, Q = N * 2;
const long INF = 0x3f3f3f3f;
struct ty
{
    long t, w, next;
    bool operator < (const ty &a) const
    {
        if (w > a.w) return true;
			else return false;
    }
};
priority_queue<ty> q;
long n, t, m;
long head[N];
ty edge[M];
bool v[N];
long dist[N];
void insertedge(long x, long y, long z, long k)
{
    edge[k].t = y;
    edge[k].w = z;
    edge[k].next = head[x];
    head[x] = k;
}
void init()
{
    memset(head, 0, sizeof(head));
    memset(edge, 0, sizeof(edge));
    m = 0;
    for (long i = 1; i <= t; i++)
    {
        long x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        insertedge(x, y, z, ++m);
    }
}
void dijkstra_priority_queue()
{
    memset(v, 0, sizeof(v));
    memset(dist, 127, sizeof(dist));
    v[1]=true;
    dist[1]=0;
    for (long i = head[1]; i != 0; i = edge[i].next)
    {
        if (edge[i].w < dist[edge[i].t])
        {
            dist[edge[i].t] = edge[i].w;
            q.push(edge[i]);
        }

    }
    ty t1, t2;
    while(!q.empty() )
    {
        t1 = q.top();
        q.pop();
        if (v[t1.t]) continue;
        v[t1.t] = true;
        for (long j = head[t1.t]; j != 0; j = edge[j].next)
        {
            long u = edge[j].t;
            if ((!v[u]) && (dist[u] > edge[j].w + dist[t1.t]))
            {
                dist[u] = edge[j].w + dist[t1.t];
                t2.t = u;
                t2.w = dist[u];
                q.push(t2);
            }
        }
    }

    cout << dist[n] << endl;
}
int main()
{
    while(scanf("%d%d", &n, &t) != EOF)
    {
        init();
        dijkstra_priority_queue();
    }
    return 0 ;
}


  
  
  
全部评论

相关推荐

05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
点赞 评论 收藏
分享
喜欢疯狂星期四的猫头鹰在研究求职打法:短作业优先
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务