POJ3159 Candies
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/C
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
这题的条件很简单就是
db-da<=x;
但是这题必须用栈的思想去解题,用队列去做会超时,我也不知道为什么??
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stack> #define N 1000001 using namespace std; int n,m,tt; int head[30001]; struct node { int x,y,z; int next; } q1[150001]; int dis[30001],v[30001]; void add(int xx,int yy,int zz) { q1[tt].x=xx; q1[tt].y=yy; q1[tt].z=zz; q1[tt].next=head[xx]; head[xx]=tt; tt++; } void spfa() { stack<int>q; for(int i=0;i<=n;i++) { dis[i]=N; v[i]=0; } v[1]=1; dis[1]=0; q.push(1); while(!q.empty()) { int ff=q.top(); q.pop(); v[ff]=0; for(int i=head[ff];i!=-1;i=q1[i].next) { if(dis[q1[i].y]>dis[ff]+q1[i].z) { dis[q1[i].y]=dis[ff]+q1[i].z; if(v[q1[i].y]==0) { q.push(q1[i].y); v[q1[i].y]=1; } } } } printf("%d\n",dis[n]); } int main() { int xx,yy,zz; while(scanf("%d%d",&n,&m)!=EOF) { tt=0; memset(head,-1,sizeof(head)); while(m--) { scanf("%d%d%d",&xx,&yy,&zz); add(xx,yy,zz); } spfa(); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构