K - Candies(差分约束)

本文介绍了一个经典的差分约束系统问题,通过一个幼儿园老师分配糖果的故事背景引入。文章详细阐述了如何利用SPFA算法来解决该问题,以确保每个孩子得到的糖果数量符合他们之间的相对公平要求,同时最大化两个特定孩子之间糖果数量的差距。
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.

这是第一道差分约束题目,用spfa求最短路径。
约束条件:
dis[v]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
   int to, v, next;
}t[152121];
int n, m;
int dis[34567];
int head[34567];
bool v[30980];
int top;
void Initial()//初始化
{
   for(int i=0;i<=n;i++)
   {
     v[i] = false;
     dis[i] = INF;
     head[i] = -1;
   }
   top = 1;
}
void spfa()//最短路
{
  stack<int>q;//××××注意,这里用到栈,刚开始的时候用的是queue,但是超时了。两者的区别不是很大,视情况而定,一般是使用队列
  q.push(1);
  v[1] = true;
  dis[1] = 0;
  while(!q.empty())
  {
      int k = q.top();
      q.pop();
      v[k] = false;
      for(int i=head[k];i!=-1;i=t[i].next)
      {
          if(dis[t[i].to]>t[i].v+dis[k])//更新(差分约束条件)
          {
              dis[t[i].to]=t[i].v+dis[k];
              if(!v[t[i].to])
              {
                q.push(t[i].to);
                v[t[i].to] = true;
              }
          }
      }
  }
  cout<<dis[n]<<endl;
}
int main()
{
     int a, b, c;
   scanf("%d %d", &n, &m);
   Initial();
   for(int i=1;i<=m;i++)
   {
     scanf("%d %d %d", &a, &b, &c);
     t[top].to = b;
   t[top].v = c;
   t[top].next = head[a];
   head[a] = top++;
   }
   spfa();
   return 0;
}

```markdown ### **题目重述** 代码 `window = Counter(candies[0, k-1])` 报错,如何改正? --- ### **详解** 该语句: ```python window = Counter(candies[0, k-1]) ``` 出现了 **语法错误**,原因是: Python 的列表(`list`)不支持用逗号 `,` 分隔的元组形式索引(如 `[0, k-1]`),这种写法只适用于 NumPy 数组或张量(如 PyTorch/TensorFlow)。对于普通列表,必须使用 **切片(slice)** 来截取子序列。 --- #### ✅ 正确写法应为: ```python window = Counter(candies[0:k]) ``` 或更简洁地: ```python window = Counter(candies[:k]) ``` 这表示:取出 `candies` 列表中从索引 `0` 到 `k-1` 的前 `k` 个元素,构建一个频率计数器。 --- #### 🔍 示例说明 假设: ```python candies = [1, 2, 2, 3, 4, 3] k = 3 ``` 则: ```python candies[:k] # -> [1, 2, 2] Counter(candies[:k]) # -> Counter({2: 2, 1: 1}) ``` 这才是正确的滑动窗口初始状态。 --- #### 🚫 常见错误对比 | 错误写法 | 正确写法 | 说明 | |--------|--------|------| | `candies[0, k-1]` | `candies[0:k]` | 列表不支持元组索引 | | `candies[0:k-1]` | `candies[:k]` | 少取了一个元素,长度不足 k | --- ### **知识点** 1. **Python 切片语法 `[:k]`** 获取列表前 k 个元素,索引范围为 0 到 k-1。 2. **Counter 统计频次** `Counter(list)` 返回元素与其出现次数的字典,适用于哈希计数。 3. **列表索引限制** 普通 list 不支持 `arr[i,j]` 形式索引,仅支持整数和切片。 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值