Candies(差分约束_栈+SPFA)
来源:互联网 发布:电脑java软件下载 编辑:程序博客网 时间:2024/06/10 19:00
CandiesCrawling in process...Crawling failedTime Limit:1500MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
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 integersN 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 throughN. snoopy and flymouse were always numbered 1 and N. Then followM lines each holding three integers A, B and c in order, meaning that kidA 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
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <queue>#include <stack>#include <algorithm>#define inf 9999999using namespace std;int head[30010];int dis[30010];int vis[30010];int n,m,i;int cnt;struct node{ int v,w; int next;} edge[150000];void add(int u,int v,int w)//加边{ edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;}void SPFA(){ memset(dis,inf,sizeof(dis)); memset(vis,0,sizeof(vis)); stack<int>G;//队列 dis[1]=0; vis[1]=1; G.push(1); while(!G.empty()) { int u=G.top(); G.pop(); vis[u]=0; for(i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(dis[v]>dis[u]+edge[i].w) { dis[v]=dis[u]+edge[i].w; if(!vis[v]) { vis[v]=1; G.push(v); } } } } printf("%d\n",dis[n]);}int main(){ int u,v,w; while(~scanf("%d %d",&n,&m)) { cnt=0; memset(head,-1,sizeof(head)); for(i=1; i<=m; i++) { scanf("%d %d %d",&u,&v,&w); add(u,v,w); } SPFA(); } return 0;}
- Candies(差分约束_栈+SPFA)
- poj3159-Candies差分约束-spfa+栈
- poj Candies(差分约束,spfa栈)
- POJ 3159 Candies 差分约束 spfa+栈+邻接表
- POJ 3159 Candies(SPFA+栈)差分约束
- POJ3159 Candies(差分约束+SPFA的栈实现)
- POJ 3159 Candies 差分约束-SPFA栈实现
- Candies 3159----差分约束系统+spfa
- POJ 3159 Candies 差分约束+spfa
- poj 3159 Candies 差分约束 + spfa
- Candies+差分约束+spfa+poj
- POJ 3159 Candies【差分约束+SPFA】
- poj 3259 Candies spfa 差分约束
- POJ 3159 Candies [差分约束系统 SPFA+STACK]
- poj 3159 Candies(差分约束 spfa stack实现)
- POJ 3159 Candies(spfa最短路,差分约束)
- POJ 3159 Candies(差分约束基础题,栈式SPFA)
- poj3159 Candies 差分约束+spfa+模拟stack
- 原版win7 install.wim在哪?
- 有限狀態機FSM coding style整理 (SOC) (Verilog) http://www.cnblogs.com/oomusou/archive/2011/06/05/fsm_coding
- POJ 2482 Stars in Your Window(线段树扫描线)
- 协同过滤实战
- projecteuler---->problem=35----Circular primes
- Candies(差分约束_栈+SPFA)
- hdu 2492 Ping pong
- 论东方花都核辐射手段
- hdoj 1455 Sticks 【dfs】
- Length of Last Word Java
- view: draw,onDraw,dispatchDraw
- CSS 盒子的产生
- Android 创建桌面快捷方式
- tomcat部署web项目的3中方法