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的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
随机推荐
- OC8_setter方法展开
// // Person.h // OC8_setter方法展开 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zh ...
- 8个应该去逛逛JQuery的学习网站
根据国外科技网站 W3Techs 一项调查了近100万个网站数据显示,jQuery是目前最流行的 JavaScript 库.对于初学者来说,有的时候很难找到一个好的学习jQuery的网站,所以本文收集 ...
- 跨域访问JSONP CORS
一.JSONP 常用的Jquery框架支持jsonp方式请求,该方式只支持GET方法,传参大小有限,而且需要后台根据jsonp的请求方式进行封装结果返回. 其中参数jsonp默认为callback,j ...
- Poj 2586 / OpenJudge 2586 Y2K Accounting Bug
1.Link: http://poj.org/problem?id=2586 2.Content: Y2K Accounting Bug Time Limit: 1000MS Memory Lim ...
- android浪漫樱花凋零动态壁纸应用源码
android浪漫樱花凋零动态壁纸应用源码,是从那个安卓教程网拿过来的,本项目是一套基于安卓的樱花动态壁纸项目源码,安装以后桌面没有图标,但是可以在修改壁纸-动态壁纸中找到.我的分辨率是480×854 ...
- xamarin android——数据绑定到控件(二)
本示例为通过媒体内容提供器获取本机中的图片显示在Gallery中. 活动中简单的初始化代码 private void InitGallery() { Gallery gallery = FindVie ...
- struts2使用struts2-bootstrap-plugin插件
1.下载插件 http://code.google.com/p/struts2-bootstrap/ 2.添加maven依赖 <dependency> <groupId>com ...
- linux实现nginx按照日期存储日志
通过shell脚本实现+定时任务+nginx信号管理实现日志按日期存储. 1.编写shell脚本,实现日志按日期存储 #!/bin/bash base_path='/home/wwwlogs/' lo ...
- Oracle 执行计划说明
生成SQL的执行计划是Oracle在对SQL做硬解析时的一个非常重要的步骤,它制定出一个方案告诉Oracle在执行这条SQL时以什么样的方式访问数据:索引还是全表扫描,是Hash Join还是Nest ...
- butterknife7.0.1使用
1.官网:http://jakewharton.github.io/butterknife/ Introduction Annotate fields with @Bind and a view ID ...