博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3159(KB4-K 差分约束)
阅读量:4950 次
发布时间:2019-06-11

本文共 3274 字,大约阅读时间需要 10 分钟。

Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 33283   Accepted: 9334

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 Mlines 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 21 2 52 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

, Sempr
 
1 //2017-08-29 2 #include 
3 #include
4 #include
5 #include
6 #include
7 #include
8 9 using namespace std;10 11 const int N = 50010;12 const int M = 250010;13 const int INF = 0x3f3f3f3f;14 15 int head[N], tot;16 struct Edge{17 int to, next, w;18 }edge[M];19 20 void init(){21 tot = 0;22 memset(head, -1, sizeof(head));23 }24 25 void add_edge(int u, int v, int w){26 edge[tot].w = w;27 edge[tot].to = v;28 edge[tot].next = head[u];29 head[u] = tot++;30 }31 32 int n, m;33 bool vis[N];34 int dis[N], cnt[N];35 int sk[N], top;//手写栈36 bool spfa(int s, int n){37 for(int i = 1; i <= n; i++){38 vis[i] = 0;39 cnt[i] = 0;40 dis[i] = INF;41 }42 vis[s] = 1;43 dis[s] = 0;44 cnt[s] = 1;45 top = 0;46 sk[top++] = s;47 while(top){48 int u = sk[--top];49 vis[u] = 0;50 for(int i = head[u]; i != -1; i = edge[i].next){51 int v = edge[i].to;52 if(dis[v] > dis[u] + edge[i].w){53 dis[v] = dis[u] + edge[i].w;54 if(!vis[v]){55 vis[v] = 1;56 sk[top++] = v;57 if(++cnt[v] > n)return false;58 }59 }60 }61 }62 return true;63 }64 65 int main()66 {67 //freopen("inputK.txt", "r", stdin);68 while(scanf("%d%d", &n, &m)!=EOF){69 init();70 int u, v, w;71 while(m--){72 scanf("%d%d%d", &u, &v, &w);73 add_edge(u, v, w);74 }75 spfa(1, n);76 printf("%d\n", dis[n]);77 }78 79 return 0;80 }

 

转载于:https://www.cnblogs.com/Penn000/p/7447839.html

你可能感兴趣的文章
JAVA程序猿怎么才干高速查找到学习资料?
查看>>
使用axel下载百度云文件
查看>>
Qt中图像的显示与基本操作
查看>>
详解软件工程之软件测试
查看>>
WCF(二) 使用配置文件实现WCF应用程序
查看>>
【CodeForces 803 C】Maximal GCD(GCD+思维)
查看>>
python 去掉换行符或者改为其他方式结尾的方法(end='')
查看>>
数据模型(LP32 ILP32 LP64 LLP64 ILP64 )
查看>>
REST构架风格介绍:状态表述转移
查看>>
struct {0}初始化
查看>>
c++ operator
查看>>
apache 添加 ssl_module
查看>>
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
getQueryString
查看>>
Servlet文件上传和下载的复习
查看>>
JavaScript笔记——正则表达式
查看>>
iOS PushMebaby
查看>>
网页消息类
查看>>
【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)
查看>>