| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
dijksta 32ms#include<iostream>
#include<stdio.h>
#include<vector>
#include<memory.h>
#include<queue>
using namespace std;
const int Max = 1<<30;
int M,N;
int Min;
int minlevel,maxlevel;
int Price[20000];
int Level[20000];
struct node{
node(){}
node(int x,int y){
value = x;
v = y;
}
bool operator <(const node rhs)const {
return value>rhs.value;
}
int value;
int v;
};
priority_queue<node> Q;
vector<vector<node> >G;
int dijkstra(){
int res = Max;
Q.push(node(0,1));//设置为酋长开始
while (!Q.empty()){
node cur = Q.top();
Q.pop();
int v = cur.v;
if (cur.value > res)//剪枝
continue;
int length = G[v].size();
for (int i = 0;i<length;++i){
if (Level[G[v][i].v] >= minlevel && Level[G[v][i].v] <=maxlevel )
Q.push(node(cur.value+G[v][i].value,G[v][i].v) );
}
if (cur.value + Price[cur.v] < res)
res = cur.value +Price[cur.v];
}
return res;
}
int main(){
cin>> M>>N;
int X;
int p,l,value;
Min = Max;
G.resize(N+1);
for (int i= 1;i<= N;++i){
cin >> Price[i] >> Level[i]>>X;
for (int j = 0;j<X;++j){
int id,price;
cin >> id >> price;
G[i].push_back(node(price,id));
}
}
int qiuLevel = Level[1];
for (int i = qiuLevel-M;i<=qiuLevel;++i ){
minlevel = i;
maxlevel = i+M;
int ans = dijkstra();
if(Min > ans )
Min = ans;
}
printf("%d\n",Min);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator