poj 3159(spfa最短路径)

Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 23152   Accepted: 6234

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

POJ Monthly--2006.12.31, Sempr

题目就是求从1到n的最短路径。

AC代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#include<stack>
using namespace std;
int n,m;
struct Node{
    int u,v,w;
    int next;
}Edge[150010];
int head[30010];
int k;
int spfa(){
    stack <int> st;
    int vis[30010],dis[30010];
    for(int i=1;i<=n;i++){
        dis[i]=999999;
        vis[i]=0;
    }
    dis[1]=0;
    st.push(1);
    while(!st.empty()){
        int u=st.top(); st.pop();
        vis[u]=0;

        for(int i=head[u];i;i=Edge[i].next){
            int v=Edge[i].v;
            if(dis[v]>dis[u]+Edge[i].w){
                dis[v]=dis[u]+Edge[i].w;
                if(!vis[v]){
                    st.push(v);
                    vis[v]=1;
                }
            }
        }

    }
    return dis[n];
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        head[i]=0;
    for(int i=1;i<=m;i++){
        int x,y,s;
        scanf("%d%d%d",&x,&y,&s);
        k++;
        Edge[k].u=x; Edge[k].v=y; Edge[k].w=s;
        Edge[k].next=head[x];
        head[x]=k;
    }
    printf("%d\n",spfa());
    return 0;
}
时间: 2024-10-07 13:30:29

poj 3159(spfa最短路径)的相关文章

poj 3259Wormholes (spfa最短路径)

#include<stdio.h> #include<string.h> #include<limits.h> #include<queue> using namespace std; #define N 5505 #define M 55000//注意边和点集的数组大小 struct edge { int to,value,next; }edges[M]; int heads[N],len=0; int addedge(int u,int v,int w)

POJ 3159 Candies(差分约束系统)

题目地址:POJ 3159 第一发差分约束的题..就当作最短路来做了...直接建图+spfa..不过我用的spfa+slf优化都超时..看了讨论区里的..把spfa换成栈就过了... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include

poj 3259(bellman最短路径)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30169   Accepted: 10914 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

[2016-04-08][POJ][3159][Candies]

时间:2016-04-08 18:05:09 星期五 题目编号:[2016-04-08][POJ][3159][Candies] 题目大意:n个小孩,m条信息,每条信息a ,b ,c,表示b小孩得到的糖不能比a小孩c个,问n小孩比1小孩最多能多多少个, 分析:直接就是求1到n的最短路(如果不是最短路,那么其他边,总有一种情况不能满足最短路这条边的情况,),直接跑一次Dijkstra 遇到的问题:使用'vector'存图TLE了,改成用数组实现的邻接表才A #include <queue> #i

poj 2253 (dis最短路径)

Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24979   Accepted: 8114 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her,

poj 1125 (floyed 最短路径)

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26395   Accepted: 14558 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

poj 1062 (dij最短路径)

昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35968   Accepted: 10314 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000

POJ 3159 Candies(SPFA+栈)差分约束

题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖? 差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,就可以 有负环时,用栈比队列快 数组开小了,不报RE,报超时 ,我晕 #include <iostream> #include <cstdlib> #include <cstdio>

poj 3159 candies (差分约束 spfa+stack)

http://poj.org/problem?id=3159 题意:一个班有n个人 每人分到若干糖果 且u的糖果数不能比v少w个 求第1个人与第n个人最大数量差 照着模板spfa+queue果断tle了 之后照着题解说的把queue改成stack就过了 但是还不明白为什么会快 而且如果用数组直接模拟会比stl更快 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>