GBX的Graph(最短路)
Problem B: Graph
Time Limit: 2 Sec Memory Limit: cid=1000&pid=1&langmask=16">Submit id=1010">Status
128 MB
Submit: 1 Solved: 1
[
Board]
Description
There are n vertexs and m directed edges. Every vertex has a lowercase letters .Now, ZZT stand at 1.
he want to go to n to find ZZ.But ZZT dont like character strings "cnm" ,"tmd","nsb". so he won't walk such a continuous
3 vertex,the first vertex is 'c' second vertex is 'n' last vertex is 'm'; He wanted to find her as soon as possible.
Input
The first line in the input will contain the number of cases ,Each case begins with two integer n,m(2<=n<=100,m<=1000)
then follow a line contain a character string "a1a2a3a4a5...an" ai is the i vertex's lowercase letter.
then follow m line ,each line contain li,ri,ci , a edge connect li and ri cost time ci(1<=li,r1<=n,1<=ci<=100);
Output
for each case ,output a integer express the minimum time, if can't find ZZ output "-1"(without quotes);
Sample Input
1
4 4
cnmc
1 2 1
2 3 1
1 3 4
3 4 1
Sample Output
5
题意:给你一个n个点m条边的有向图,每个点都有一个小写字母。
如今ZZT站在点1。ZZ站在点n。ZZT想用最短的路程走到ZZ的地方。可是呢,ZZT不希望走过这种连续的三点:cnm,tmd,nsb。如今问你他是否能到达ZZ所在地。
若能,输出最短路径,否则输出-1。
分析:此题关键在于怎样构图。
我们能够把边当点来使用,那么总共同拥有m个点。
增加一个源点0连向以点1发出去的边,增加一个汇点m+1使得点全部指向点n的边连向点m+1,那么原题就变成了从点0開始到达点m+1的最短路径。构图时,当两条边(u,v)。(x,y)中 v == x 而且(str[u],str[v],str[y]) !=(c,n,m),(t,m,d),(n,s,b),则可连接。
构图完成。跑一遍最短路就可以。
题目链接:http://192.168.4.140/problem.php?
cid=1000&pid=1
代码清单:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull; const int maxn = 100 + 5;
const int maxv = 1000 + 5;
const int max_dis=0x7f7f7f7f; struct Edge{
int to;
int dis;
Edge(){}
Edge(int to,int dis){
this -> to = to;
this -> dis = dis;
}
}; struct E{ int u,v,dis; }edge[maxv]; int T;
int n,m;
int a,b,c;
bool vis[maxv];
int dist[maxv];
char str[maxn];
vector<Edge>graph[maxv]; void init(){
for(int i=0;i<maxv;i++) graph[i].clear();
} void input(){
scanf("%d%d%s",&n,&m,str);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].dis);
}
} bool judge(int pre,int now,int to){
if(str[pre]=='c'&&str[now]=='n'&&str[to]=='m') return true;
if(str[pre]=='t'&&str[now]=='m'&&str[to]=='d') return true;
if(str[pre]=='n'&&str[now]=='s'&&str[to]=='b') return true;
return false;
} void createGraph(){
for(int i=1;i<=m;i++){
if(edge[i].u==1) graph[0].push_back(Edge(i,edge[i].dis));
if(edge[i].v==n) graph[i].push_back(Edge(m+1,0));
for(int j=1;j<=m;j++){
if(i==j) continue;
if(edge[i].v==edge[j].u){
if(judge(edge[i].u-1,edge[i].v-1,edge[j].v-1))
continue;
graph[i].push_back(Edge(j,edge[j].dis));
}
}
}
} int spfa(){
memset(vis,false,sizeof(vis));
memset(dist,max_dis,sizeof(dist));
queue<int>q;
while(!q.empty()) q.pop();
vis[0]=true; dist[0]=0;
q.push(0);
while(!q.empty()){
int u=q.front(); q.pop();
vis[u]=false;
for(int i=0;i<graph[u].size();i++){
int v=graph[u][i].to;
int d=graph[u][i].dis;
if(dist[v]>dist[u]+d){
dist[v]=dist[u]+d;
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
}
if(dist[m+1]==max_dis) return -1;
return dist[m+1];
} void solve(){
createGraph();
printf("%d\n",spfa());
} int main(){
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
scanf("%d",&T);
while(T--){
init();
input();
solve();
}return 0;
}
GBX的Graph(最短路)的更多相关文章
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
- 2018牛客网暑假ACM多校训练赛(第十场)F Rikka with Line Graph 最短路 Floyd
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round10-F.html 题目传送门 - https://www.n ...
- HDU-4725 The Shortest Path in Nya Graph 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- CSU1659: Graph Center(最短路)
Description The center of a graph is the set of all vertices of minimum eccentricity, that is, the s ...
- HDU 4725 The Shortest Path in Nya Graph (最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinc ...
随机推荐
- 紫书 例题 10-15 UVa 1638(递推)
从大到小安排杆子 分三种情况 (1)插到最左边,那么左边看到了杆子会多一个 (2)插到最右边,那么右边看到了杆子会多一个 (3)插到中间边,那么不影响左边和右边看到的杆子数 具体看代码 #includ ...
- difference in physical path, root path, virutal path, relative virtual path, application path and aboslute path?
http://stackoverflow.com/questions/13869817/difference-in-physical-path-root-path-virutal-path-relat ...
- 26.angularJS $routeProvider
转自:https://www.cnblogs.com/best/tag/Angular/ O'Reilly书上的伪代码 var someModule = angular.module('someMod ...
- centos7 ssh免口令认证登录
摘要:centos7, xshell, 公钥, ssh ssh登录方式有口令认证登录和密钥认证登录 接下来本次介绍是ssh密钥登录方式 (1)产生公钥 (2)将公钥放置到centos7的(/root ...
- Spring MVC 待学习---新特性
Spring3.1新特性 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类. 二.Spring2.5引入注解式处理器支持,通过@Controller ...
- 关于Javascript的forEach 和 map
本篇博客转载自 https://blog.fundebug.com/2018/02/05/map_vs_foreach/ 如果你已经有使用JavaScript的经验,你可能已经知道这两个看似相同的方法 ...
- Flask Flash闪现
Flash介绍以及工作方式 flash中添加消息 取出flash中的消息 Flash介绍以及工作方式 - 介绍: flash :闪现 一个好的应用和用户界面都需要良好的反馈. 如果用户得不到足够的反馈 ...
- BZOJ 2730 矿场搭建 Tarjan求割点
思路: Tarjan求出来点双&割点 判一判就行了 //By SiriusRen #include <stack> #include <cstdio> #include ...
- HDU 5358 First One 数学+尺取法
多校的题,摆明了数学题,但是没想出来,蠢爆了,之前算了半天的s[i][j]的和,其实是积.其实比赛的时候我连log(s[i][j])+1是s[i][j]的位数都没看出来,说出来都丢人. 知道了这个之后 ...
- IDEA集成Python插件,SDK配置
(第一次写在博客园添加随笔, 会有些生硬,有不对的地方和描述错误之处希望可以指出) 最近在学习一个新的知识点,涉及到Python的使用,因为第一次接触很多地方都不是很明白 ,好,废话不多说,现在直接上 ...