poj3159

Dijkstra算法

Posted by SHIELD-SKY on August 18, 2016

Candies(Dijkstra算法+优先队列)

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

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

这道题主要基本思路就是用堆来优化,我根据《挑战程序设计竞赛》上优化的思路写了个代码超时了:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <limits>


using namespace std;
const int MAX_V = 30010;
const int INF = INT_MAX;

struct edge { int to, cost; };

typedef pair<int, int> P; //first是最短距离,second是顶点的编号

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

int N, M;

void dijkstra(int s)
{
    priority_queue< P, vector<P>, greater<P> > que;
    fill(d, d + V, INF);
    d[s] = 0;
    que.push(P(0, s));
    
    while(!que.empty())
    {
        P p = que.top(); que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        
        for(int i = 0; i < G[v].size(); i++)
        {
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost)
            {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    
    scanf("%d %d", &N, &M);
    for(int i = 1; i <= M; i++)
    {
        int s;
        edge t;
        scanf("%d %d %d", &s, &t.to, &t.cost);
        G[s].push_back(t);
    }
    V = N + 1;
    dijkstra(1);
    printf("%d\n", d[V-1]);
    return 0;
}

然后看郭炜老师的代码AC了,两个代码的差异还没屡清楚,先把郭老师的代码帖这吧:

//by guo wei
#include <cstdio> 
#include <iostream> 
#include <vector> 
#include <queue> 
#include <cstring> 

using namespace std;

struct CNode {
	int k; //有向边的终点
	int w; //权值,或当前k到源点的距离
};

bool operator < ( const CNode & d1, const CNode & d2 ) {  return d1.w > d2.w; } //这里越大的优先级越小,最小的元素出列,priority_queue默认总是将最大的元素出列 
priority_queue<CNode> pq;

bool bUsed[30010]={0};

vector<vector<CNode> > v; //v是整个图的邻接表 
const unsigned int INFINITE = 100000000;

int main() 
{
	int N,M,a,b,c;
	int i,j,k;
	CNode p;
	scanf("%d%d", & N, & M ); 
	v.clear();
	v.resize(N+1);
	memset( bUsed,0,sizeof(bUsed)); 
	for( i = 1;i <= M; i ++ ) 
	{
		scanf("%d%d%d", & a, & b, & c); 
		p.k = b;
		p.w = c;
		v[a].push_back( p);
	}
	p.k = 1; //源点是1号点
	p.w = 0; //1号点到自己的距离是0 
	pq.push (p);
 

	while( !pq.empty ()) { 
		p = pq.top ();
		pq.pop();
		if( bUsed[p.k]) //已经求出了最短路
			continue; 
		bUsed[p.k] = true;
		if( p.k == N ) //因只要求求1-N的最短路,所以要break 
			break;
		for( i = 0, j = v[p.k].size(); i < j; i ++ ) 
			{ 
				CNode q; 
				q.k = v[p.k][i].k;
				if( bUsed[q.k] ) 
					continue;
				q.w = p.w + v[p.k][i].w ;
				pq.push (q); //队列里面已经有q.k点也没关系
			} 
		}
		printf("%d", p.w ); //优先队列最后弹出后,while最后一次循环完成后,p.w即是所求
return 0;
}

8月18日23时23分更新:

半夜与学弟交流,崔学弟的代码也AC了,也是学到了新东西,链式前向星表示图

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 30010, maxm = 150010;
const int INF = 0x3f3f3f;
int head[maxn], nxt[maxm], v[maxm], w[maxm];
int vis[maxn], d[maxn], tot = 0, n, m;
void add_edge(int a, int b, int c) {
  nxt[++tot] = head[a];
  v[tot] = b; w[tot] = c;
  head[a] = tot;
}
void dijsktra() {
  priority_queue<pii, vector<pii>, greater<pii> > Q;
  memset(d, INF, sizeof(d));
  d[1] = 0;
  memset(vis, 0, sizeof(vis));
  Q.push(make_pair(0, 1));
  while(!Q.empty()) {
    pii x = Q.top(); Q.pop();
    int u = x.second;
    if(vis[u]) continue;
    vis[u] = 1;
    for(int i = head[u]; i != -1; i = nxt[i])
      if(d[v[i]] > d[u] + w[i]) {
        d[v[i]] = d[u] + w[i];
        Q.push(make_pair(d[v[i]], v[i]));
      }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  int a, b, c;
  for(int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    add_edge(a, b, c);
  }
  dijsktra();
  printf("%d\n", d[n]);
  return 0;
}