题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是差分约束的经典题,条件可以转化为dist[b]<=dist[a]+c,于是a->b直接连边,边权值为c,从而题目转化为图上求1->n的最短路,看了一下数据,30000个点,150000条边,果断用Dijkstra+priority_queue,1300MS+险过,orz.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAXN 30030
#define inf 1<<30
typedef pair<int,int>Pair; struct Edge{
int v,w;
Edge(int vv,int ww):v(vv),w(ww){}
}; int dist[MAXN];
bool mark[MAXN];
int n,m; vector<vector<Edge> >map; int Dijkstra(int u)
{
memset(mark,false,(n+)*sizeof(bool));
for(int i=;i<=n;i++)dist[i]=inf;
dist[u]=;
priority_queue<Pair,vector<Pair>,greater<Pair> >Q;
Q.push(make_pair(,u));
while(!Q.empty()){
Pair pp=Q.top();
Q.pop();
int dd=pp.first,u=pp.second;
if(mark[u])continue;
mark[u]=true;
for(int i=;i<map[u].size();i++){
int v=map[u][i].v,w=map[u][i].w;
if(mark[v])continue;
if(dd+w<dist[v]){
dist[v]=dd+w;
Q.push(make_pair(dist[v],v));
}
}
}
return dist[n];
} int main()
{
int u,v,w;
scanf("%d%d",&n,&m);
map.clear();map.resize(n+);
while(m--){
scanf("%d%d%d",&u,&v,&w);
map[u].push_back(Edge(v,w));
}
printf("%d\n",Dijkstra());
return ;
}
04-27 04:35