Problem B: Graph

Time Limit: 2 Sec  Memory Limit:
128 MB

Submit: 1  Solved: 1

[

cid=1000&pid=1&langmask=16">Submit][

id=1010">Status][Web
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. 1
  2. 4 4
  3. cnmc
  4. 1 2 1
  5. 2 3 1
  6. 1 3 4
  7. 3 4 1

Sample Output

  1. 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

代码清单:

  1. #include<set>
  2. #include<map>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<cctype>
  8. #include<string>
  9. #include<cstdio>
  10. #include<cstdlib>
  11. #include<cstring>
  12. #include<iostream>
  13. #include<algorithm>
  14. using namespace std;
  15.  
  16. typedef long long ll;
  17. typedef unsigned int uint;
  18. typedef unsigned long long ull;
  19.  
  20. const int maxn = 100 + 5;
  21. const int maxv = 1000 + 5;
  22. const int max_dis=0x7f7f7f7f;
  23.  
  24. struct Edge{
  25. int to;
  26. int dis;
  27. Edge(){}
  28. Edge(int to,int dis){
  29. this -> to = to;
  30. this -> dis = dis;
  31. }
  32. };
  33.  
  34. struct E{ int u,v,dis; }edge[maxv];
  35.  
  36. int T;
  37. int n,m;
  38. int a,b,c;
  39. bool vis[maxv];
  40. int dist[maxv];
  41. char str[maxn];
  42. vector<Edge>graph[maxv];
  43.  
  44. void init(){
  45. for(int i=0;i<maxv;i++) graph[i].clear();
  46. }
  47.  
  48. void input(){
  49. scanf("%d%d%s",&n,&m,str);
  50. for(int i=1;i<=m;i++){
  51. scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].dis);
  52. }
  53. }
  54.  
  55. bool judge(int pre,int now,int to){
  56. if(str[pre]=='c'&&str[now]=='n'&&str[to]=='m') return true;
  57. if(str[pre]=='t'&&str[now]=='m'&&str[to]=='d') return true;
  58. if(str[pre]=='n'&&str[now]=='s'&&str[to]=='b') return true;
  59. return false;
  60. }
  61.  
  62. void createGraph(){
  63. for(int i=1;i<=m;i++){
  64. if(edge[i].u==1) graph[0].push_back(Edge(i,edge[i].dis));
  65. if(edge[i].v==n) graph[i].push_back(Edge(m+1,0));
  66. for(int j=1;j<=m;j++){
  67. if(i==j) continue;
  68. if(edge[i].v==edge[j].u){
  69. if(judge(edge[i].u-1,edge[i].v-1,edge[j].v-1))
  70. continue;
  71. graph[i].push_back(Edge(j,edge[j].dis));
  72. }
  73. }
  74. }
  75. }
  76.  
  77. int spfa(){
  78. memset(vis,false,sizeof(vis));
  79. memset(dist,max_dis,sizeof(dist));
  80. queue<int>q;
  81. while(!q.empty()) q.pop();
  82. vis[0]=true; dist[0]=0;
  83. q.push(0);
  84. while(!q.empty()){
  85. int u=q.front(); q.pop();
  86. vis[u]=false;
  87. for(int i=0;i<graph[u].size();i++){
  88. int v=graph[u][i].to;
  89. int d=graph[u][i].dis;
  90. if(dist[v]>dist[u]+d){
  91. dist[v]=dist[u]+d;
  92. if(!vis[v]){
  93. vis[v]=true;
  94. q.push(v);
  95. }
  96. }
  97. }
  98. }
  99. if(dist[m+1]==max_dis) return -1;
  100. return dist[m+1];
  101. }
  102.  
  103. void solve(){
  104. createGraph();
  105. printf("%d\n",spfa());
  106. }
  107.  
  108. int main(){
  109. // freopen("cin.txt","r",stdin);
  110. // freopen("cout.txt","w",stdout);
  111. scanf("%d",&T);
  112. while(T--){
  113. init();
  114. input();
  115. solve();
  116. }return 0;
  117. }

GBX的Graph(最短路)的更多相关文章

  1. Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...

  2. 2018牛客网暑假ACM多校训练赛(第十场)F Rikka with Line Graph 最短路 Floyd

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round10-F.html 题目传送门 - https://www.n ...

  3. HDU-4725 The Shortest Path in Nya Graph 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  4. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  5. 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 ...

  6. 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 ...

  7. CSU1659: Graph Center(最短路)

    Description The center of a graph is the set of all vertices of minimum eccentricity, that is, the s ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. JSTL标准标签库具体解释

    JSTL标签库的使用是为类弥补html表的不足.规范自己定义标签的使用而诞生的. 在告别modle1模式开发应用程序后.人们開始注重软件的分层设计,不希望在jsp页面中出现java逻辑代码,同一时候也 ...

  2. Codeforces 558C Amr and Chemistry 全都变相等

     题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...

  3. KVM硬件辅助虚拟化之 EPT in Nested Virtualization

    在嵌套虚拟环境(Nested Virtualization)下,执行在hypervisor上的Virtual Machine仍能够作为hypervisor去执行其他的Virutal Machine,而 ...

  4. win32下实现透明窗体

    最開始写透明窗体的代码,在百度了之后,找到了SetLayeredWindowAttributes()这一个函数,可是因为网上案列的缺少,使得非常多人无法非常好的使用这一个方法,我花了几天的时间写了一个 ...

  5. linux设备驱动归纳总结(三):4.ioctl的实现

    linux设备驱动归纳总结(三):4.ioctl的实现 一.ioctl的简单介绍: 尽管在文件操作结构体"struct file_operations"中有非常多相应的设备操作函数 ...

  6. thinkphp5项目--企业单车网站(五)

    thinkphp5项目--企业单车网站(五) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  7. MVP模式入门(结合Rxjava,Retrofit)

    本文MVP的sample实现效果: github地址:https://github.com/xurui1995/MvpSample 老规矩,在说对MVP模式的理解之前还是要再谈谈MVC模式,了解了MV ...

  8. powerpoint(ppt) 的制作

    1. 幻灯片母版 所谓母版,是共享的部分,也即想在全部幻灯片重复出现的元素. 首先,幻灯片母版在菜单栏的[视图]选择[幻灯片母版]显示和查看. 通过幻灯片母版的编辑和设计,可进一步: 在幻灯片的相关位 ...

  9. Weka中数据挖掘与机器学习系列之Weka简介(二)

    不多说,直接上干货! Weka简介 Weka是怀卡托智能分析环境(Waikato Environment for Knowledge Analysis)的英文字首缩写,官方网址为:http://www ...

  10. Asp.Net碎知识

    在aspx页面 获取值: UserModel user=new UserModel();实例化 user.Address=context["txtAddress"]; 如果前台不需 ...