-Ackerman博客-Ackerman

Candies

Toretto·2019-08-04 00:54·480 次阅读

Candies

 

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 AB 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.
 
思路:这题是个典型的差分约束题,转化一下其实就是求最短路。
但是这题坑在它不能用SPFA+queue的写法   必须要用SPFA+手写栈!(疯狂TL)
 
复制代码
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 
 7 const int MAXN=30010;
 8 const int MAXE=150010;
 9 const int INF=0x3f3f3f3f;
10 int head[MAXN];//每个结点的头指针
11 int vis[MAXN];//在队列标志
12 
13 int Q[MAXN];//堆栈
14 int dist[MAXN];
15 
16 struct Edge
17 {
18     int to;
19     int v;
20     int next;
21 }edge[MAXE];
22 int tol;
23 void add(int a,int b,int v)//加边
24 {
25     edge[tol].to=b;
26     edge[tol].v=v;
27     edge[tol].next=head[a];
28     head[a]=tol++;
29 }
30 void SPFA(int start,int n)
31 {
32     int top=0;
33     for(int v=1;v<=n;v++)//初始化
34     {
35         if(v==start)
36         {
37             Q[top++]=v;
38             vis[v]=true;
39             dist[v]=0;
40         }
41         else
42         {
43             vis[v]=false;
44             dist[v]=INF;
45         }
46     }
47     while(top!=0)
48     {
49         int u=Q[--top];
50         vis[u]=false;
51         for(int i=head[u];i!=-1;i=edge[i].next)
52         {
53             int v=edge[i].to;
54             if(dist[v]>dist[u]+edge[i].v)
55             {
56                 dist[v]=dist[u]+edge[i].v;
57                 if(!vis[v])
58                 {
59                     vis[v]=true;
60                     Q[top++]=v;
61                 }
62             }
63         }
64     }
65 }
66 int main()
67 {
68    // freopen("in.txt","r",stdin);
69   //  freopen("out.txt","w",stdout);
70     int n;
71     int M;
72     int a,b,c;
73     while(scanf("%d%d",&n,&M)!=EOF)
74     {
75         tol=0;//加边计数,这个不要忘
76         memset(head,-1,sizeof(head));
77         while(M--)
78         {
79             scanf("%d%d%d",&a,&b,&c);
80             //b-a<=c
81             add(a,b,c);
82             //大-小<=c ,有向边(小,大):c
83         }
84         SPFA(1,n);
85         printf("%d\n",dist[n]);
86     }
87     return 0;
88 }
复制代码

 

关于SPFA 它死了 

复制代码
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <queue>
 7 
 8 #define INF 0x3f3f3f3f
 9 #define pii pair<int,int>
10 using namespace std;
11 const int MAXN = 2e5+7;
12 
13 int head[MAXN];
14 int dist[MAXN];
15 bool vis[MAXN];
16 int cnt;
17 
18 struct Edge{
19     int to,val,next;
20 }edge[MAXN];
21 
22 void init(){
23     cnt = 0;
24     memset(head,-1, sizeof(head));
25     memset(vis,false, sizeof(vis));
26     memset(dist,INF, sizeof(dist));
27 }
28 
29 void add(int u,int v,int w){
30     edge[cnt].to = v;
31     edge[cnt].val = w;
32     edge[cnt].next = head[u];
33     head[u] = cnt++;
34 }
35 
36 void dijstra(int s)
37 {
38     priority_queue<pii,vector<pii>,greater<pii> > q;
39     dist[s] = 0;
40     q.push({dist[s],s});
41     while (!q.empty())
42     {
43         int now = q.top().second;
44         q.pop();
45         if (vis[now])
46             continue;
47         vis[now] = true;
48         for (int i=head[now];i!=-1;i=edge[i].next)
49         {
50             int v = edge[i].to;
51             if (dist[v]>dist[now]+edge[i].val)
52             {
53                 dist[v] = dist[now] + edge[i].val;
54                 q.push({dist[v],v});
55             }
56         }
57     }
58 }
59 
60 int main()
61 {
62     int n,m,s;
63     while (~scanf("%d%d",&n,&m))
64     {
65         init();
66         while (m--)
67         {
68             int a,b,c;
69             scanf("%d%d%d",&a,&b,&c);
70             add(a,b,c);
71         }
72         dijstra(1);
73         printf("%d\n",dist[n]);
74     }
75     return 0;
76 }
复制代码

 

posted @   _Ackerman  阅读(480)  评论(0编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用
点击右上角即可分享
微信分享提示