poj3159 Candies

POJ - 3159

Candies

题目大意:

  给n个小孩发糖,有m个条件

  每个条件给出a,b,c,使得v[b]-v[a]<=c

  求v[n]-v[1]的最大值

Sample Input

2 2
1 2 5
2 1 4

Sample Output

5
/*
    差分约束裸题。
    差分约束的基本模式为,给出若干个形如a-b<=c的不等式,问你x-y的最大值是多少
    那么对每个不等式a-b<=c,连一条由b指向a权值为c的有向边,所有的不等式构成一个图
    那么答案就是y到x的最短路
*/
#include<iostream>
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=3000010;
const int maxm=3500000;
const int INF=999999;
int n,m,head[maxn],num,dis[maxn];
bool vis[maxn];
struct node{
    int to,v,pre;
}e[maxn];
void add(int from,int to,int v){
    e[num].to=to;
    e[num].v=v;
    e[num].pre=head[from];
    head[from]=num++;
}
void spfa(){
    int s=1;
    stack<int>q;
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    while(!q.empty()){
        int cur=q.top();
        q.pop();
        vis[cur]=0;
        for(int i=head[cur];i!=-1;i=e[i].pre){
            int id=e[i].to;
            if(dis[id]>dis[cur]+e[i].v){
                dis[id]=dis[cur]+e[i].v;
                if(!vis[id]){
                    q.push(id);
                    vis[id]=1;
                }
            }
        }
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++){
            head[i]=-1;
            dis[i]=INF;
            vis[i]=0;
        }
        num=0;
        for(int i=0;i<m;i++){
            int from,to,v;
            scanf("%d%d%d",&from,&to,&v);
            add(from,to,v);
        }
        spfa();
        printf("%d\n",dis[n]);
    }
    return 0;
}
时间: 2024-08-08 01:26:39

poj3159 Candies的相关文章

poj3159 Candies(差分约束,dij+heap)

poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d[v] – d[u] <= w. 再看题目给出的关系:b比a多的糖果数目不超过c个,即d[b] – d[a] <= c ,正好与上面模型一样, 所以连边a->b,最后用dij+heap求最短路就行啦. ps:我用vector一直TLE,后来改用前向星才过了orz... 1 #include&

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

poj3159 Candies 2012-09-07

http://poj.org/problem?id=3159 差分约束系统 (spfa+ 队列)会TLE,(spfa+stack)可以过,不过有点奇葩的是建边时 输入 (a,b,c) 如果连边 w(b,a)=c 然后 做 spfa(n),输出dis[1]会超时.反过来 连边 w(a,b)=c,然后做spfa(1),输出dis[n] 就484Ms过了.数据比较坑 爹吧. 1 Program poj3159; 2 3 type cord=record 4 5 ne,da,po:longint; 6

poj3159——Candies(差分约束+SPFA堆栈)

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 ofte

差分约束Poj3159 Candies

没负环.直接搞就行,但是 spfa 队列会超时. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <cstdio> #include <cstdl

[poj3159]Candies(差分约束+链式前向星dijkstra模板)

题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与dijkstra中的d[v]>=d[u]+C相同,即可求解. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream>

北大ACM暑期培训课程目录(三)

本文出自:http://blog.csdn.net/svitter 一.图的生成树 .必然含有n-1条边. .无向带权图. .如何求最小生成树. .prime | kruskal Prime: 从点里面找一个最短的边. kruskal: 从边里面找. .密集图使用邻接矩阵来存储. .稀疏图用邻接表来存储. .开一个邻接表,一个vector的一维数组. 时间复杂度(ElogV) 一般情况下邻接表更加优秀 题目:poj1258(邻接矩阵) 使用prime+堆完成.priority_queue(就是二

最短路径 专题总结

一.模板: 1.dijsktra算法(矩阵): 1 int n,cost[1005][1005],dis[1005],vis[1005],dp[1005]; 2 void dijkstra(int st) 3 { 4 memset(vis,0,sizeof(vis)); 5 for(int i = 1; i<=n; i++) 6 dis[i] = (i==st?0:INF); 7 8 for(int i = 1; i<=n; i++) 9 { 10 int k, minn = INF; 11

【POJ3159】Candies 裸的pqspfa模版题

不多说了,就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 30500 #define M 200000 #define