描述
N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.
输入
The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
S is the source city, 1 <= S <= N
D is the destination city, 1 <= D <= N
L is the road length, 1 <= L <= 100
T is the toll (expressed in the number of coins), 0 <= T <=100
Notice that different roads may have the same source and destination cities.
输出
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.
我知道你们不看英语题面….
翻译:
题目大意:
一个直男癌因为他女友和她打牌的时候作弊,所以他们分手了,他现在在1,想去n,通过每一条路都得花钱,他现在有k块钱。
输入:
k,n,m,为总钱数,点数,边数。
接下来m行,每行4个整数,a,b,c,d,即从a号点到b号点有一条权值为c的单向边,花费为d
输出:
一个整数,为他在花不完k块钱的情况下从1到n的最短路,如果不存在这样一条路,puts(“-1”);
样例输入
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
样例输出
11
所以说,这个题用DJ可以做….SPFA的话,数据有bug,打个奇怪的姿势就能水过,只能说这个题的数据出水了。
你如果用SPFA的奇怪的姿势水过了,那么我建议你把样例输入的第6行和第⑨行换一下再试试。
如果用DFS版SPFA,那么肯定会T,我试过。
那么我们就需要dijsktra了。
每搜到一个点,就把它的边权和花费加上扔堆里。每当你从堆里取出一个点的时候,如果它现在的花费大于k,那就忽略掉。根据DJ的性质,如果搜到了n,输出当前权值直接return,在此之前立个flag,if搜到了n,令flag=1,else,puts(“-1”)。
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=200000;
const int inf=0x7fffffff;
struct Edge
{
int to;
int d;
int next;
}edge[maxn];
struct meico
{
int p;
int d;
int c;
bool operator <(const meico &hah)const
{
return d>hah.d;
}
};
int head[maxn],dist[maxn];
int k,n,m;
bool vis[maxn];
int fee[maxn];
int tot;
void add(int f,int t,int d)
{
edge[++tot]=(Edge){t,d,head[f]};
head[f]=tot;
}
priority_queue<meico>q;
int ans;
int flag;
void DJ()
{
while(!q.empty())
{
meico hah=q.top();
q.pop();
if(hah.c>k)
continue;
if(hah.p==n)
{
flag=1;
printf("%d\n",hah.d);
return;
}
for(int i=head[hah.p];i;i=edge[i].next)
{
Edge e=edge[i];
q.push((meico){e.to,hah.d+e.d,hah.c+fee[i]});
}
}
}
int main()
{
scanf("%d%d%d",&k,&n,&m);
for(int i=2;i<=n;i++)
dist[i]=inf;
for(int i=1;i<=m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
add(a,b,c);
fee[tot]=d;
}
meico hah;
hah.p=1;
hah.c=0;
hah.d=0;
q.push(hah);
DJ();
if(!flag)
puts("-1");
return 0;
}