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 cin 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
题意:N个人,M条边,u,v,w,规定v最多只能比u多w,问第一个人最多比第N个人少多少
题解:一开始领会错了意思,以为是要求出每个人的上限,用了floyd,后来才发现这只是1与N之间的战斗。
差分约束;dist[v]<=dist[u]+cost,所以条件写成if(dist[v]>dist[u]+cost),是不是眼熟?没错…差分了之后与原来的最短路条件是一样的。
这里上两个代码:
傻子vector超时版
#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<string,pair<int,int> > pii; typedef long long ll; const ll mod = 1000000007; const int INF = 0x3f3f3f3f; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int MAXN=1000010; struct qnode //优先队列的创建(储存节点v以及到源点的距离C) { int v;//节点v int c;//源点的距离(用来排序) qnode(int _v=0, int _c=0):v(_v),c(_c){} //初始化为0??? bool operator<(const qnode &r)const//定义优先队列的排序方式(实际上可以用pair进行替代),先比较第一维,相等时比较第二维 { return c>r.c;} }; struct Edge//边结构 { int v,cost; //储存边以及权 Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}//初始化??? }; vector<Edge>E[MAXN];//看作二维数组,每个i数组储存着从i边到下一边的所有数据 bool vis[MAXN];//标记数组 int dist[MAXN];//离源点的距离 void Dijkstra(int n, int start) //点的编号从1开始,n是点的个数,start是源点 { memset(vis, false, sizeof(vis));//初始化标记数组 for(int i = 1; i <= n;i++)dist[i] = INF;//初始化距离数组 priority_queue<qnode>que;//创建定义的优先队列,类型为qnote while(!que.empty()) que.pop();//清空队列(这里不明白为什么要清空,是赋了初值吗?那为什么要赋初值) dist[start]=0;//源点到源点的距离初始化为0,以便于得出后续 que.push(qnode(start,0));//源点入队,距离为0 qnode tmp;//qnode类型的中转变量 while(!que.empty()){//只要队列不为空,也可以看作每次出队都是一轮松弛 tmp=que.top();//将队列中d最小的赋给tmp que.pop();//出队 int u = tmp.v;//方便后续的操作,令u=该点的序号 if(vis[u])continue;//如果已被标记,那么跳过 vis[u]=true;//标记 for(int i=0;i<E[u].size();i++){//循环u的下一点的个数次 int v=E[tmp.v][i].v;//v=(点u数组中的下一点) int cost=E[u][i].cost; //cost = (点u数组中到下一点的距离) if(!vis[v]&&dist[v]>dist[u]+cost){ //松弛操作 dist[v]=dist[u]+cost; que.push(qnode(v,dist[v]));//入队 } } } } void addedge(int u,int v,int w){ E[u].push_back(Edge(v,w)); } int n,m,u,v,w; int main(){ scanf("%d%d",&n,&m); rep(i,1,m){ scanf("%d%d%d", &u,&v,&w); addedge(u,v,w); } Dijkstra(n,1); printf("%d\n", dist[n]); return 0; }
kuangbin数组修正版:head[u]储存的是u点的边的编号,如果head[u]=-1,说明你已经穷尽了以u点开始的边,结构体中的next就是为了储存上一条以u起始的边。可以看到,每次next被赋予的是head[u],在输入时,它表示上一条边的编号,记得初始化为-1.
//============================================================================ // Name : POJ.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <queue> #include <vector> using namespace std; /* * 使用优先队列优化Dijkstra算法 * 复杂度O(ElogE) * 注意对vector<Edge>E[MAXN]进行初始化后加边 */ const int INF=0x3f3f3f3f; const int MAXN=30010; struct qnode { int v; int c; qnode(int _v=0,int _c=0):v(_v),c(_c){} bool operator <(const qnode &r)const { return c>r.c; } }; struct Edge { int v,cost; int next; }; Edge edge[200000]; int tol; int head[MAXN]; bool vis[MAXN]; int dist[MAXN]; void Dijkstra(int n,int start)//点的编号从1开始 { memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++)dist[i]=INF; priority_queue<qnode>que; while(!que.empty())que.pop(); dist[start]=0; que.push(qnode(start,0)); qnode tmp; while(!que.empty()) { tmp=que.top(); que.pop(); int u=tmp.v; if(vis[u])continue; vis[u]=true; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; int cost=edge[i].cost; if(!vis[v]&&dist[v]>dist[u]+cost) { dist[v]=dist[u]+cost; que.push(qnode(v,dist[v])); } } } } void addedge(int u,int v,int w) { edge[tol].v=v; edge[tol].cost=w; edge[tol].next=head[u]; head[u]=tol++; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n,m; while(scanf("%d%d",&n,&m)==2) { tol=0; memset(head,-1,sizeof(head)); int A,B,C; while(m--) { scanf("%d%d%d",&A,&B,&C); addedge(A,B,C); } Dijkstra(n,1); printf("%d\n",dist[n]); } return 0; }