spoj 338

2024-06-10 17:32
文章标签 spoj 338

本文主要是介绍spoj 338,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意: 无向图  每条边有长度和费用两个属性  求从点1到点n 在花费不超过 k 的情况下的最短路径

BFS  使用优先队列 长度短的优先出列      题解上的方法没看懂  不知道怎么用链表维护 .....



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>using namespace std;struct node
{int dd, pp, len;node(int d, int p, int le){dd = d, pp = p, len = le;}bool operator < (const node & p) const{if(len != p.len)return p.len < len;return p.pp < pp;}
};
vector<int> gto[110], cost[110], Len[110];
bool vis[110][10010];
int main()
{int T,N,K,R,ans;scanf("%d",&T);while(T--){ans = -1;memset(vis, false, sizeof(vis));scanf("%d%d%d",&K,&N,&R);for(int i = 1; i <= N; i++){gto[i].clear(), cost[i].clear(), Len[i].clear();}for(int i = 0; i < R; i++){int s,d,l,t;scanf("%d%d%d%d",&s,&d,&l,&t);gto[s].push_back(d), Len[s].push_back(l), cost[s].push_back(t);}priority_queue<node> q;q.push(node(1, 0, 0));while(!q.empty()){node u = q.top();q.pop();if(u.dd == N){ans = u.len;break;}if(!vis[u.dd][u.pp]){vis[u.dd][u.pp] = true;int p = gto[u.dd].size();for(int i = 0; i < p; i++){int de = gto[u.dd][i];int _len = u.len + Len[u.dd][i];int co = u.pp + cost[u.dd][i];if(co <= K)q.push(node(de, co, _len));}}}printf("%d\n",ans);}return 0;
}


这篇关于spoj 338的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048812

相关文章

SPOJ - QTREE (树链剖分)

基础的树链剖分题目,不过是边权,可以向下映射成点权或者按边剖分。 VIEW CODE #include <iostream>#include<stdio.h>#include<cmath>#include<string.h>#include<algorithm>#include<string>using namespace std;const int mmax=100

338. Counting Bits 比特位计数

https://leetcode-cn.com/problems/counting-bits/description/ Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary represent

数位统计DP——AcWing 338. 计数问题

数位统计DP 定义 数位DP(Digital DP)是一种用于解决与数字的数位相关问题的动态规划算法。它将数字的每一位看作一个状态,通过转移状态来计算满足特定条件的数字个数或其他相关统计信息。 运用情况 统计满足特定条件的数字个数,例如在给定范围内有多少个数字满足某些数位特征。计算数字的某个数位上的特定统计信息,如出现的数字频率等。解决与数字排列、组合相关的问题。 注意事项 数位DP的

spoj 227

留着 #include <cstdio>#include <cstring>#include <cstdlib>#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1const int MAXN = 200010;int pos[MAXN];int sum[MAXN << 2];// = MAXN*4int ans[

spoj 416

又臭又长的烂代码 ...... #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define maxn 1010using namespace std;char a[1010];int num[10],last;//bool cc(int sum)

spoj 1108

要求输出一个牌的顺序 使每隔1、2、.....、n翻牌后出现1 2 3 4 5 6 7 8 9 .... n 将牌想象成n个空格  正向推 空n个位置放n 循环 需优化 #include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#define maxn 300

spoj 379

题是水题  但丫的题目意思太难懂 .......   英语水平  ...... #include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;int a[100010];int main(){int n;while(scanf("%d",&n) && n){for(i

spoj 62

看了题解  自己好水   ...... #include <cstdio>#include <cstdlib>struct node{int x,y;};node A,B;node add(node &a,node &b){node f;f.x = a.x+b.x;f.y = a.y+b.y;return f;}node re(node &a,node &b){nod

spoj 1436

用并查集看一下是否会围成一个环  若围成环直接输出NO   当然 当 m >= n  时必然会围成环直接输出NO #include <algorithm>#include <cstdio>#include <cstring>using namespace std;int f[10010];int find(int x){return f[x] == x ? x : f[x] = fi

spoj 42

简单题   水水~~ /*************************************************************************> Author: xlc2845 > Mail: xlc2845@gmail.com> Created Time: 2013年10月24日 星期四 13时33分17秒**********************