POJ 3159 Candies 差分约束系统(这题卡SPFA的队列的双端队列)

来源:互联网 发布:夜奔什么意思网络内涵 编辑:程序博客网 时间:2024/05/22 10:57

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

Hint

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


题目翻译:

给出n个点,m个约束条件,每个条件表示为A,B,c,意思是B-A<=c,问1和n至少差多少

题解:

A向B连边权为c的边,跑最短路即可

原理是我们求最短路的时候是要保证dis[v]<=dis[u]+len,因为一旦有dis[v]>dis[u]+len,我们就会更新dis[v],使得其满足条件,因此,我们相当于是要满足B<=A+c,因此就是从A(对应u)连向B(对应v)连边权为c(对应len)的边,然后跑最短路,因为跑最短路的时候可以满足最苛刻的条件,也就是满足最小的条件


这道题双端队列和单向STL队列都会T掉

#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int MAXN = 30000 + 10;int n, m, A, B, c, tail, head[MAXN], dis[MAXN];struct Line{ int to, nxt, flow; }line[ 150000 + 5 ];int q[25000000];bool vis[MAXN];void add_line( int from, int to, int flow ) {    line[++tail].to = to;    line[tail].nxt = head[from];    line[tail].flow = flow;    head[from] = tail;}void SPFA() {    int h = 12500001, t = 12500000;    memset( dis, 0x3f, ( n + 1 ) * sizeof( int ) );    dis[1] = 0; q[++t] = 1; vis[1] = true;    while( h <= t ) {        int u = q[h]; h++; vis[u] = false;        for( register int i = head[u]; i; i = line[i].nxt ) {            int v = line[i].to;            if( dis[v] > dis[u] + line[i].flow ) {                dis[v] = dis[u] + line[i].flow;                if( vis[v] ) continue;                if( dis[v] < dis[q[h]] ) q[--h] = v;                else                     q[++t] = v;                vis[v] = true;             }        }    }}int main( ) {    while( scanf( "%d%d", &n, &m ) != EOF ) {        memset( head, 0, ( n + 1 ) * sizeof( int ) ); tail = 0;        for( register int i = 1; i <= m; i++ ) {            scanf( "%d%d%d", &A, &B, &c );            add_line( A, B, c );        }        SPFA();        printf( "%d\n", dis[n] );    }    return 0;}

下面是stack写的A的代码,我感到很不舒服,为什么DFS就可以过


#include <cstdio>#include <cstring>#include <iostream>#include <stack>using namespace std;const int MAXN = 30000 + 10;int n, m, A, B, c, tail, head[MAXN], dis[MAXN];struct Line{ int to, nxt, flow; }line[ 150000 + 5 ];bool vis[MAXN];void add_line( int from, int to, int flow ) {    line[++tail].to = to;    line[tail].nxt = head[from];    line[tail].flow = flow;    head[from] = tail;}void SPFA() {    stack<int>s; while( !s.empty() ) s.pop();     memset( dis, 0x3f, sizeof( dis ) );    s.push(1); vis[1] = true; dis[1] = 0;    while( !s.empty() ) {        int u = s.top(); s.pop(); vis[u] = false;        for( register int i = head[u]; i; i = line[i].nxt ) {            int v = line[i].to;            if( dis[v] > dis[u] + line[i].flow ) {                dis[v] = dis[u] + line[i].flow;                if( vis[v] ) continue;                s.push( v );                vis[v] = true;             }        }    }}int main( ) {    while( scanf( "%d%d", &n, &m ) != EOF ) {        memset( head, 0, sizeof( head ) ); tail = 0;        for( register int i = 1; i <= m; i++ ) {            scanf( "%d%d%d", &A, &B, &c );            add_line( A, B, c );        }        SPFA();        printf( "%d\n", dis[n] );    }    return 0;}

这里写图片描述

原创粉丝点击