POJ 3159 Candies 差分约束系统(这题卡SPFA的队列的双端队列)
2017-10-13 09:04
501 查看
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 21 2 5
2 1 4
Sample Output
5Hint
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 ); } 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 ); } return 0; }
相关文章推荐
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- POJ 3159 Candies(差分约束系统 spfa+stack)
- POJ 3159-Candies(差分约束系统-SPFA+邻接表)
- POJ 3159 Candies(差分约束系统)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies(spfa最短路,差分约束)
- poj 3159 Candies 【简单差分约束】 【SPFA + 数组模拟栈】
- POJ 3159 Candies(dijkstra+heap&spfa+stack)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 Candies(差分约束系统)
- poj 3159 Candies (spfa+stack)
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- poj 3159 Candies (差分约束系统裸题)
- POJ 3159 Candies 解题报告(Dijkstra & SPFA) 及 双向Dijkstra性能测试
- POJ 3159 Candies(差分约束 转spfa+stack 求最短路径)
- POJ 3159 Candies 还是差分约束(栈的SPFA)
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ 3159 差分约束系统+spfa
- POJ 3159 Candies 差分约束系统