wordpress域名网站搬家,怎样做才能发布你的网站,侧边栏jquery网站后台,2022年大连黄页题意#xff1a;
给n个小朋友分发糖果#xff0c;但小朋友们之间有嫉妒心。接下来m行#xff0c;每行三个数#xff0c;分别表示小朋友A希望B得到的糖果不能比他多x个。要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖。
题目#xff1a;
During the ki…题意
给n个小朋友分发糖果但小朋友们之间有嫉妒心。接下来m行每行三个数分别表示小朋友A希望B得到的糖果不能比他多x个。要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖。
题目
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.
分析
1.这道题目是典型的差分约束系统小于等于是最短路 大于等于是最长路核心在于xj-xibk会发现它类似最短路中的三角不等式d[v]d[u]w[u,v]即d[v]-d[u]w[u,v]即从u指向v建边num[B]num[A]x。求最大值的话就是跑1-n的最短路对应求最小值就是跑1-n的最长路。此题用队列的话会T掉可以改成栈或者想办法优化。 2.关于差分约束选择队列还是堆栈当存在回路SPFA的队列实现会超时堆栈实现可以堆栈实现SPFA有时候堆栈确实比较快 都大三的老阿姨了差分约束还不熟练加油吧查缺补漏中。。。。
AC代码
#includestdio.h
#includestring.h
#includealgorithm
using namespace std;
const int inf0x3f3f3f3f;
const int M3e410;
const int N15e410;
int n,m,k,a,b,c;
int vis[M]/**在队列标志*/;
int head[M]/**每个结点的头指针(或者编号)*/;
int q[M]/**堆栈*/,dis[M];
struct node
{int b;int c;int next;
}edge[N];void add(int a,int b,int c)//加边
{edge[k].bb;edge[k].cc;edge[k].nexthead[a];head[a]k;
}
void SPFA(int start,int n)
{int top0;for(int i1;in;i)//初始化{if(istart){q[top]i;//入栈vis[i]true;dis[i]0;}else{vis[i]false;dis[i]inf;}}while(top!0){int aq[--top];vis[a]false;for(int ihead[a];i!-1;iedge[i].next){int bedge[i].b;if(dis[b]dis[a]edge[i].c){dis[b]dis[a]edge[i].c;if(!vis[b]){vis[b]true;q[top]b;}}}}
}
int main()
{while(~scanf(%d%d,n,m)){k0;//加边计数这个不要忘memset(head,-1,sizeof(head));for(int i0;im;i){scanf(%d%d%d,a,b,c);add(a,b,c);}SPFA(1,n);//printf(*****\n);printf(%d\n,dis[n]);//此处求n比1最多多多少糖果}return 0;
}
/**给n个人派糖果给出m组数据每组数据包含A、B、c 三个数
意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数 c 。
最后求n 比 1 最多多多少糖果。*/