这个做法是错误的!!!!数据太水,就水过去了。
要看正解请看另一个题目的解法
题目意思很简单,就是求费用最短路,数据比较小,考虑暴力bfs。
状态定义为(花费,最短路,终点)的一个三元组。
优先队列按照最短路长度排序,每次从队列里取出最短路长度最短的状态,然后把跟该状态终点相邻的点加入队列。因为数据比较小,所以不用记录访问情况也能ac。
#pragma warning(disable:4996) #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int N = 105; vector<int>g[N], e[N], cost[N]; struct node { int cost, len, v; node(){} node(int cost,int len,int v):cost(cost),len(len),v(v){} bool operator<(const node&op)const { return len > op.len; } }; int n, k; void add(int u, int v, int c, int cc) { g[u].push_back(v); e[u].push_back(c); cost[u].push_back(cc); } int bfs() { priority_queue<node>q; q.push(node(0, 0, 1)); while (!q.empty()) { node now = q.top(); q.pop(); int u = now.v; if (u == n)return now.len;//如果是所求终点的话直接返回就是符合费用条件的最短路了 for (int i = 0; i < (int)g[u].size(); i++) { int to = g[u][i]; if (now.cost + cost[u][i] <= k) {//如果符合费用条件就加入队列 q.push(node(now.cost + cost[u][i], now.len + e[u][i], to)); } } } return -1; } int main() { int R; scanf("%d %d %d", &k, &n, &R); while (R--) { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); add(a, b, c, d); //add(b, a, c, d); } printf("%d\n", bfs()); return 0; }