POJ 3159 Candies(Dijkstra演算法+優先佇列+堆)
Candies
Time Limit: 1500MS | Memory Limit: 131072K |
Total Submissions: 37796 | Accepted: 10637 |
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
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
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
struct CNode{
int k,w;
bool operator<(const CNode &d1)const{
return w>d1.w;
}
};
priority_queue<CNode> pq;
bool bUsed[30010]={0};
vector<vector<CNode> > v;//v是整個圖的鄰接表
const int INFINITE=100000000;
int main(){
int N,M;
int a,b,c;
scanf("%d%d",&N,&M);
CNode p;
v.clear();//鄰接表清空
v.resize(N+1);//鄰接表擴容
memset(bUsed,0,sizeof(bUsed));//訪問陣列標記
for(int i=1;i<=M;i++){
scanf("%d %d %d",&a,&b,&c);
p.k=b;
p.w=c;
v[a].push_back(p);//將p放入鄰接表
}
//針對訪問自己的情況
p.k=1;
p.w=0;
pq.push(p);//將p按照優先佇列的規則加入pq
while(!pq.empty()){
p=pq.top();
pq.pop();
if(bUsed[p.k])//求出了最短路
continue;
bUsed[p.k]=true;//標記為已訪問
if(p.k==N)//保證不形成迴路
break;
for(int 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);
}
}
printf("%d\n",p.w);
return 0;
}
思路:主要是用vector容器構造一個鄰接表,用來儲存每一行的資訊。那麼怎麼把優先佇列和鄰接表聯絡起來呢?
優先佇列的主要操作:
常用的操作如下:
empty() 如果優先佇列為空,則返回真
pop() 刪除第一個元素
push() 加入一個元素
size() 返回優先佇列中擁有的元素的個數
top() 返回優先佇列中有最高優先順序的元素
push_back函式,在vector類中作用為在vector尾部加入一個數據。
我們把每一行的三個資料a,b,c輸入進去,b和c分別定義為p.k和p.w,然後從尾部依次加入鄰接表p
然後我們把p中的元素依次加入優先佇列pq,按照優先佇列規定的規則進行排序,執行top和pop操作。
因為STL優先佇列存在侷限性,那就是隻提供入隊、出隊、取得隊首元素的值的功能,而dijkstra演算法的堆優化需要能夠隨機訪問佇列中某個節點(來更新源點節點的最短距離)。所以我們需要進行遍歷,對權值進行更新操作。在權值更新完成之後,輸出找出的最小權值,結束程式。