【CF】142 Div.1 B. Planes
SPFA.注意状态转移条件,ans的求解需要在bfs中间求解。因为只要到了地点n,则无需等待其他tourist。
还是蛮简单的,注意细节。
/* 229B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e5+;
const int INF = 0x3f3f3f3f;
vpii E[maxn];
vpii T[maxn];
int dis[maxn];
bool visit[maxn];
int a[maxn], b[maxn]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, m;
int u, v, w; scanf("%d %d", &n, &m);
while (m--) {
scanf("%d %d %d", &u, &v, &w);
E[u].pb(mp(v, w));
E[v].pb(mp(u, w));
}
rep(i, , n+) {
scanf("%d", &m);
rep(j, , m+)
scanf("%d", &a[j]);
b[m] = ;
per(j, , m) {
if (a[j]+ == a[j+])
b[j] = b[j+] + ;
else
b[j] = ;
}
rep(j, , m+)
T[i].pb(mp(a[j], b[j]));
} int i, tmp;
int ans = INF;
pii p(, );
vpii::iterator iter;
queue<int> Q;
Q.push();
memset(dis, 0x3f, sizeof(dis));
// may be have 0 in T[1]
iter = upper_bound(all(T[]), p);
if (iter!=T[].end() && iter->fir==) {
dis[] = iter->sec;
} else {
dis[] = ;
}
visit[] = true; while (!Q.empty()) {
u = Q.front();
Q.pop();
visit[u] = false;
for (i=; i<SZ(E[u]); ++i) {
v = E[u][i].fir;
w = E[u][i].sec;
tmp = dis[u] + w;
if (v==n && tmp<ans)
ans = tmp;
p.fir = tmp;
iter = upper_bound(all(T[v]), p);
if (iter!=T[v].end() && iter->fir==tmp) {
tmp += iter->sec;
}
if (tmp < dis[v]) {
dis[v] = tmp;
if (!visit[v]) {
visit[v] = true;
Q.push(v);
}
}
}
} ans = ans==INF ? -:ans;
printf("%d\n", ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【CF】142 Div.1 B. Planes的更多相关文章
- 【CF】121 Div.1 C. Fools and Roads
题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...
- 【CF】310 Div.1 C. Case of Chocolate
线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...
- 【CF】110 Div.1 B. Suspects
这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...
- 【CF】222 Div.1 B Preparing for the Contest
这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...
- 【CF】207 Div.1 B.Xenia and Hamming
这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...
- 【CF】196 Div.2 D. Book of Evil
显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...
- 【CF】223 Div.1 C Sereja and Brackets
水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...
- 【CF】259 Div.1 B Little Pony and Harmony Chest
还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...
- 【CF】174 Div.1 B Cow Program
思路是树形DP+状态压缩.其实仅有2个状态,奇数次来到x或者偶数次来到x.(因为对x的更新不同).同时开辟visit数组,解决环.注意,一旦遇到环结果就是-1.DP数组存放第奇数/偶数次来到x时,对y ...
随机推荐
- MVC小系列(十三)【全局异常处理与异常日志】
在MVC网站的global.asax中的Application_Start方法里,有这样一段代码 protected void Application_Start() { //它的主要作用是将全局过滤 ...
- .net中压缩和解压缩的处理
最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...
- js一些算法实现
1.约瑟夫环实现 //附有调试 function joseph(n,p){ var arr=[]; for(var i=0;i<n;i++){ arr.push(i); } debugger; ...
- .NET Entity Framework入门简介及简单操作
Entity Framework是微软借鉴ORM思想开发自己的一个ORM框架. ORM就是将数据库表与实体对象(相当于三层中的Model类)相互映射的一种思想. 最大的优点就是非常方便的跨数据库平台. ...
- php中怎么实现后台执行?
http://www.cnblogs.com/zdz8207/p/3765567.html php中实现后台执行的方法: ignore_user_abort(true); // 后台运行set_tim ...
- MathType需要安装一个较新版本的MT Extra(True type)字体[转]
MathType 6.0中MT Extra(TrueType)字体问题在打开MathType6.0时,有时会提示MathType需要安装一个较新版本的MT Extra(TrueType)字体,这是因为 ...
- bootstrap 正则表达式
<asp:TextBox runat="server" title="邮箱正确格式:xxx@xxx.xxx" class="form-cont ...
- ECMAScript 6 中的一些新特性
1.箭头函数,直接写出来v =>看不出来什么,但是跟传统写法一比较,很直观地就能看出v =>是代替了匿名函数 function(v)的写法,{}与逻辑照旧,但是要注意,=与>之间不能 ...
- 开始学习requirejs+easyui的使用.
开始学习requirejs+easyui的使用. 目录结构: |-project |-easyui01 |-js |-main.js |-index.html |-libs libs目录下放入的是ea ...
- 线段树(单点更新)HDU1166、HDU1742
在上一篇博文里面,我提到了我不会线段树,现在就努力地学习啊! 今天AC一题感觉都很累,可能是状态不佳,在做HDU1166这题目时候,RE了无数次. 原因是:我的宏定义写错了,我已经不是第一犯这种错误了 ...