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, 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.
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
POJ Monthly--2006.12.31, Sempr
#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算法的堆優化需要能夠随機通路隊列中某個節點(來更新源點節點的最短距離)。是以我們需要進行周遊,對權值進行更新操作。在權值更新完成之後,輸出找出的最小權值,結束程式。