2013
11-12

# 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?

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 one line with only the largest difference desired. The difference is guaranteed to be finite.

2 2
1 2 5
2 1 4

5

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

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

class node implements Comparable{
int v,next,disten;
node(){
v = next = disten = -1;
}
node(int v,int next,int disten){
this.v = v;
this.next = next;
this.disten = disten;
}
void set_node(int v,int next,int disten){
this.v = v;
this.next = next;
this.disten = disten;
}
void copy(node obj){
this.v = obj.v;
this.next = obj.next;
this.disten = obj.disten;
}
public int compareTo(Object obj){

node cnt = (node)obj;
if(cnt.disten< disten) return 1;
return -1;
}
}
class binary_Heap{
node tree[];
int n,p,c;
binary_Heap(){};
binary_Heap(int cap){
n = 0;
tree = new node[40000];
for(int i=0;i< 40000;++i) tree[i] = new node();
}
void push(node temp){
for(p=++n;p>1 && tree[p>>1].compareTo(temp)>0;p>>=1){
tree[p].copy(tree[p>>1]);
}
tree[p].copy(temp);

}
node front(){return tree[1];}
void pop(){
for(p=1,c=2;c< n && tree[c+=(c< n-1 && tree[c+1].compareTo(tree[c])>0)?1:0].
compareTo(tree[n])>0;tree[p].copy(tree[c]),p=c,c<<=1);

tree[p].copy(tree[n--]);
}
}
class Graph{
int n,top;
node Map[];
Graph(){}
Graph(int n){
top = n+1;
this.n = n;
Map = new node[200000];
for(int i=0;i< 200000;++i){
Map[i] = new node();
}
}
Map[top].set_node(v, Map[u].next,disten);
Map[u].next = top;
++top;
}

int get_Dijkstra(int s,int t){
node temp = new node();
int BIG = 1000000000;
int meat[] = new int[n+10];
//binary_Heap heap = new binary_Heap(n);
Queue que = new PriorityQueue< node>();

Arrays.fill(meat, BIG);

meat[s] = 0;
for(int i=Map[s].next;i>=0;i=Map[i].next){
meat[Map[i].v] = Map[i].disten;

//heap.push(Map[i]);
}
//while(heap.n>0){
while(!que.isEmpty()){
//temp.copy(heap.front());
temp.copy((node)que.element());
que.remove();
//	heap.pop();

if(meat[temp.v]==temp.disten){
for(int i=Map[temp.v].next;i>=0;i=Map[i].next){
if(meat[Map[i].v]>temp.disten+Map[i].disten){
meat[Map[i].v] = temp.disten+Map[i].disten;
//heap.push(new node(Map[i].v,-1,meat[Map[i].v]));
}
}
}
}

return meat[t];
}
}

public class Main {

static public void main(String[]args)throws Exception{

int n,m,u,v,disten;
Graph obj;

n = Get_Num(cin);
m = Get_Num(cin);

obj = new Graph(n);

for(int i=0;i< m;++i){
u = Get_Num(cin);
v = Get_Num(cin);
disten = Get_Num(cin);
}

System.out.println(obj.get_Dijkstra(1,n));
}

static int Get_Num(StreamTokenizer cin)throws Exception{
cin.nextToken();
return (int) cin.nval;
}
}

1. 这道题目虽然简单，但是小编做的很到位，应该会给很多人启发吧！对于面试当中不给开辟额外空间的问题不是绝对的，实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟，今天看到小编这篇文章相见恨晚。

2. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience