【codeforce Gym 100570B】【最短路SPFA】 ShortestPath Query 【询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同】
来源:互联网 发布:如何招聘php程序员 编辑:程序博客网 时间:2024/06/08 01:18
传送门:gym 100570B
描述:
De Prezer loves troyic paths.Consider we have a graph with n vertices and m edges.Edges are directed in one way.And there is at most one edge from any vertex to any other vertex.If there is an edge from v to u, then c(v, u) is its color and w(v, u) is its length.Otherwise,c(v, u) = w(v, u) = - 1.
A sequence p1, p2, ..., pk is a troyic path is and only if for each 1 ≤ i ≤ k, 1 ≤ pi ≤ n and if i < k, then c(pi, pi + 1) > - 1 and if i + 1 < k, then c(pi, pi + 1) ≠ c(pi + 1, pi + 2) .
The length of such troyic path is and it's called a p1 - pk path.
In such graph, length of the shortest path from vertex v to u is the minimum length of all v - u paths.(The length of the shortest path from any vertex to itself equals 0)
De Prezer gives you a graph like above and a vertex s.
De Prezer also loves query. So he gives you q queries and in each query, gives you number t and you should print the length of the shortest path from s to t (or - 1 if there is no troyic path from s to t)
The first line of input contains three integers n and m and C, the number of vertices, the numbers of edges and the number of valid colors.
The next m lines, each line contains 4 integers v, u, w(v, u), c(v, u) (1 ≤ v, u ≤ n and v ≠ u and 1 ≤ w(v, u) ≤ 109 and 1 ≤ c(v, u) ≤ C).
The line after that contains integer s and q.
The next q lines, each line contains information of one query, number t.
1 ≤ n, m, C, q ≤ 105
m ≤ n(n - 1)
1 ≤ s, t ≤ n
For each query, print the answer.
5 4 10001 2 10 12 3 10 23 4 10 24 5 10 11 512345
01020-1-1
5 5 21 2 10 12 3 10 23 4 10 14 5 10 21 5 39 11 512345
010203039
题意:
询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正。
思路:
因为路径的合法性和边的颜色有关,
所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜色,d数组表示到这条边的出点v的距离。
期望复杂度是O(km),k是边入队次数,m是边数。最后根据边来松弛顶点,O(m),总复杂度是O(km+m)。
一开始想的Dijkstra(看到边权为正。。),存点和之前边的颜色每次更新的时候判断来的那个点的颜色和当前边的颜色是否一样,WA了,很快我就意识到,入点不能只保存最短路径的颜色c1,如果边的颜色和c1一样,那么会判成路径不合法,但是实际上可能还存在一条次短路径且颜色和c1不等,所以不能只存点的颜色,应该按照题意用spfa去跑并且满足相邻边的颜色不同。
代码:
#include <bits/stdc++.h>using namespace std;#define ll __int64#define rep(i,k,n) for(int i=k;i<=n;i++)template<class T> T sqr(T x){ return x * x; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num);}const ll inf = 0x3f3f3f3f3f3f3f3fLL;const int N=1e5+10;int head[N], to[N], nxt[N], col[N], wei[N], vis[N], etol;ll d[N];//edgell dd[N];//vertexint n, m, s, t, C, q;void spfa(){ memset(d, 0x3f, sizeof(ll) * m); memset(vis, 0, sizeof(vis)); queue<int>q; for(int i = head[s]; ~i; i = nxt[i]){//把s作为入点的边先加入队列 q.push(i); vis[i] = 1; d[i] = wei[i]; } while(!q.empty()){ int e = q.front(); q.pop(); vis[e] = 0; for(int i = head[to[e]]; ~i; i = nxt[i]){ if(col[i] != col[e] && wei[i] + d[e] < d[i]){ d[i] = wei[i] + d[e]; if(!vis[i]){ q.push(i); vis[i] = 1;} } } } memset(dd+1, 0x3f, sizeof(ll) * n); dd[s]=0; rep(i, 0, m-1){ dd[to[i]] = min(dd[to[i]], d[i]); }}inline void addedge(int u, int v, int w, int c){ to[etol] = v; wei[etol] = w; col[etol] = c; nxt[etol] = head[u]; head[u] = etol++;}int main(){ read(n),read(m),read(C); memset(head+1, -1, sizeof(int) * n); rep(i, 1, m){ int u, v, w, c; read(u),read(v),read(w),read(c); addedge(u, v, w, c); } read(s),read(q); spfa(); rep(i, 1, q){ read(t); printf("%I64d\n", dd[t] == inf ? -1: dd[t]); } return 0;}
- 【codeforce Gym 100570B】【最短路SPFA】 ShortestPath Query 【询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同】
- 关于最短路径ShortestPath的几种算法
- 有如图所示的七巧板,试设计算法,使用至多4种不同颜色对七巧板进行涂色(每块七巧板一种颜色),要求相邻区域的颜色互补相同,打印输出所有可能的涂色方案。
- SDUTOJ 2498 AOE网上的关键路径 最短路spfa
- 蓝桥杯--算法练习:最短路(单源最短路径spfa算法)
- 【最短路】poj2457 SPFA+记录路径
- Bellman Ford+SPFA队列优化(路径还原 输出最短路的路径)
- CF 一个不知道题号的题。。 B. ShortestPath Query
- hdoj-2544 最短路【最短路径--dijkstra&&spfa&&floyd】
- 最短路 dijkstra 权值非负的单源最短路径
- codeforce 543 B 最短路
- codeforce 449 B 最短路
- 迷宫最短路径问题(ShortestPath)的求解——利用链式队列
- 迷宫的最短路路径
- POJ2457 Part Acquisition(Spfa最短路+记录路径)
- 最短路打印路径SPFA(good)uva11374
- ZOJ3088 Easter Holidays spfa 最长路 最短路 路径打印
- poj 2457 Part Acquisition 【spfa最短路 + STL路径输出】
- Jsoup抓取数据
- 中序、前序、后序遍历线索化二叉树
- 最大子矩阵和
- mfc 中静态控件static 的双击响应事件
- android笔记之百度地图详情页展示
- 【codeforce Gym 100570B】【最短路SPFA】 ShortestPath Query 【询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同】
- 兼容IE8/Chrome的autocomplete
- 设计模式-读书笔记-10/14
- 线程学习二:线程池执行Runnable与Callable
- Linux系统中的环境变量知识详解
- c++使用libpcre捕获多行数据
- 欢迎使用CSDN-markdown编辑器
- jsp重写Url
- Unity移动端手势操作——3种手势互斥判断