poj 3159(差分约束经典题)
题目链接: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.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<vector> 7 using namespace std; 8 #define MAXN 30030 9 #define inf 1<<30 10 typedef pair<int,int>Pair; 11 12 struct Edge{ 13 int v,w; 14 Edge(int vv,int ww):v(vv),w(ww){} 15 }; 16 17 int dist[MAXN]; 18 bool mark[MAXN]; 19 int n,m; 20 21 vector<vector<Edge> >map; 22 23 int Dijkstra(int u) 24 { 25 memset(mark,false,(n+2)*sizeof(bool)); 26 for(int i=1;i<=n;i++)dist[i]=inf; 27 dist[u]=0; 28 priority_queue<Pair,vector<Pair>,greater<Pair> >Q; 29 Q.push(make_pair(0,u)); 30 while(!Q.empty()){ 31 Pair pp=Q.top(); 32 Q.pop(); 33 int dd=pp.first,u=pp.second; 34 if(mark[u])continue; 35 mark[u]=true; 36 for(int i=0;i<map[u].size();i++){ 37 int v=map[u][i].v,w=map[u][i].w; 38 if(mark[v])continue; 39 if(dd+w<dist[v]){ 40 dist[v]=dd+w; 41 Q.push(make_pair(dist[v],v)); 42 } 43 } 44 } 45 return dist[n]; 46 } 47 48 int main() 49 { 50 int u,v,w; 51 scanf("%d%d",&n,&m); 52 map.clear();map.resize(n+2); 53 while(m--){ 54 scanf("%d%d%d",&u,&v,&w); 55 map[u].push_back(Edge(v,w)); 56 } 57 printf("%d\n",Dijkstra(1)); 58 return 0; 59 } 60 61 62