poj 1724(最短路)
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
#define MAXN 105
int n,k;
struct B
{
int end,len,toll;
friend bool operator<(B x,B y)
{
if(x.len!=y.len)
return x.len>y.len;
return x.toll>y.toll;
}
};
vector<B> v[MAXN];
priority_queue<B> pq;
int bfs()
{
int i;
B next,now;
while(!pq.empty()) pq.pop();
now.end=1;
now.len=0;
now.toll=0;
pq.push(now);
while(!pq.empty())
{
now=pq.top();
pq.pop();
if(now.end==n) return now.len;
for(i=0;i<v[now.end].size();i++)
{
if(v[now.end][i].toll+now.toll<=k)
{
next=v[now.end][i];
next.len+=now.len;
next.toll+=now.toll;
pq.push(next);
}
}
}
return -1;
}
int main()
{
int i,x,star,end,len,toll;
while(scanf("%d%d%d",&k,&n,&x)!=EOF)
{
B t;
for(i=0;i<=n;i++) v[i].clear();
for(i=1;i<=x;i++)
{
scanf("%d%d%d%d",&star,&end,&len,&toll);
t.end=end;
t.len=len;
t.toll=toll;
v[star].push_back(t);
}
printf("%d\n",bfs());
}
}
posted on 2011-06-11 12:38 thinking001 阅读(149) 评论(0) 收藏 举报
浙公网安备 33010602011771号