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的更多相关文章

  1. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  2. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  3. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  4. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  5. 【CF】207 Div.1 B.Xenia and Hamming

    这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...

  6. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

  7. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  8. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

  9. 【CF】174 Div.1 B Cow Program

    思路是树形DP+状态压缩.其实仅有2个状态,奇数次来到x或者偶数次来到x.(因为对x的更新不同).同时开辟visit数组,解决环.注意,一旦遇到环结果就是-1.DP数组存放第奇数/偶数次来到x时,对y ...

随机推荐

  1. 使用ROW_NUMBER进行的快速分页

    DECLARE @pageSize INT ; DECLARE @pageIndex INT ; SET @pageSize = 5 SET @pageIndex =2 ; --第二页,每页显示5条数 ...

  2. Use AUTO Mode with FOR XML (SQL Server 2012) Multiple Table

    SELECT Cust.CustomerID, OrderHeader.CustomerID, OrderHeader.SalesOrderID, OrderHeader.Status FROM Sa ...

  3. onConfigurationChanged与OnCreate,究竟谁被调用的问题

    在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges="keyboardHidden|orientation&qu ...

  4. NSArray 常用的一些方法

    - (NSUInteger) count; 返回数组中元素个数 - (id)objectAtIndex:(NSUInteger)index; 返回一个id类型的数组指定位置元素 - (id)lastO ...

  5. 工作中的问题解决 -- (win2003 asp.net) Session和带页面回传的方法无法正常使用解决方案

    公司BP&IT项目组.从上上个月成立开始开发BP&IT软件.这个月开始测试我悲剧的发现他尽然不支持我电脑上的IE11.半个多月还没解决 我们先来分析下原因首页 登陆页面正常浏览 htt ...

  6. Leetcode Count Prime

    Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n ...

  7. IOS 学习笔记 2015-03-20 O之 nil,Nil,NULL,NSNull

    1.oc最好 用nil   [ nil  任意方法],不会崩溃 nil 是一个对象值.NULL是一个通用指针(泛型指针). 2. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其 ...

  8. MySQL 查询某时间段范围内的数据 补零

    1.创建基础表 CREATE TABLE num (i INT); INSERT INTO num (i) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9) ...

  9. html定义对象

    <object>定义一个对象<param>为对象定义一个参数 参数的名称:name = "" 参数的值:value=""classid: ...

  10. postgresql info

    DROP TYPE IF EXISTS info CASCADE;CREATE TYPE info AS (state int4,operator varchar,time TIMESTAMP(0) ...