链接:http://vjudge.net/problem/viewProblem.action?id=24941

描述:n个点,m条边的无向图,寻找从S到T的最短路。

思路:基础的单源点最短路 用Dijkstra或spfa都可以解决

这是我的实现:

  1. 1 #include <iostream>
  2. 2 #include <cstdio>
  3. 3 #include <cstring>
  4. 4 #include <queue>
  5. 5 using namespace std;
  6. 6 #define MaxN 20020
  7. 7 #define MaxM 100020
  8. 8 struct node
  9. 9 {
  10. 10 int v,dist;
  11. 11 node *next;
  12. 12 };
  13. 13 node Edge[MaxM];
  14. 14 node *cnt=&Edge[0];
  15. 15 node *adj[MaxN];
  16. 16 int dist[MaxN];
  17. 17 int N,INF;
  18. 18 int n,m,S,T;
  19. 19 inline void Get_int(int &Ret)
  20. 20 {
  21. 21 char ch;
  22. 22 bool flag=false;
  23. 23 for(;ch=getchar(),ch<'0'||ch>'9';)
  24. 24 if(ch=='-')
  25. 25 flag=true;
  26. 26 for(Ret=ch-'0';ch=getchar(),ch>='0'&&ch<='9';Ret=Ret*10+ch-'0');
  27. 27 flag&&(Ret=-Ret);
  28. 28 }
  29. 29 inline void Clean()
  30. 30 {
  31. 31 memset(Edge,0,sizeof(Edge));
  32. 32 cnt=&Edge[0];
  33. 33 memset(adj,0,sizeof(adj));
  34. 34 }
  35. 35 inline void Addedge(int u,int v,int w)
  36. 36 {
  37. 37 node *p=++cnt;
  38. 38 p->v=v;
  39. 39 p->dist=w;
  40. 40 p->next=adj[u];
  41. 41 adj[u]=p;
  42. 42
  43. 43 p=++cnt;
  44. 44 p->v=u;
  45. 45 p->dist=w;
  46. 46 p->next=adj[v];
  47. 47 adj[v]=p;
  48. 48 }
  49. 49 inline void Read_Build()
  50. 50 {
  51. 51 Get_int(n);Get_int(m);Get_int(S);Get_int(T);
  52. 52 int i,u,v,w;
  53. 53 for(i=1;i<=m;++i)
  54. 54 {
  55. 55 Get_int(u);Get_int(v);Get_int(w);
  56. 56 Addedge(u,v,w);
  57. 57 }
  58. 58 }
  59. 59 struct cmp
  60. 60 {
  61. 61 bool operator()(node a,node b)
  62. 62 {
  63. 63 return a.dist>b.dist;
  64. 64 }
  65. 65 };
  66. 66 priority_queue <node, vector<node>, cmp> q;
  67. 67 void Dijkstra(int s)
  68. 68 {
  69. 69 node c,d;
  70. 70 node *p;
  71. 71 int i,j,k;
  72. 72 memset(dist,0x3f,sizeof(dist));
  73. 73 INF=dist[s];
  74. 74 dist[s]=0;
  75. 75 c.v=s;c.dist=0;
  76. 76 q.push(c);
  77. 77 while(!q.empty())
  78. 78 {
  79. 79 d=q.top();q.pop();
  80. 80 j=d.v;
  81. 81 for(p=adj[j];p!=NULL;p=p->next)
  82. 82 {
  83. 83 k=p->v;
  84. 84 if(dist[k]>dist[j]+p->dist)
  85. 85 {
  86. 86 dist[k]=dist[j]+p->dist;
  87. 87 d.v=k;d.dist=dist[k];
  88. 88 q.push(d);
  89. 89 }
  90. 90 }
  91. 91 }
  92. 92 }
  93. 93 inline void Print()
  94. 94 {
  95. 95 if(dist[T]==INF)
  96. 96 printf("unreachable\n");
  97. 97 else
  98. 98 printf("%d\n",dist[T]);
  99. 99 }
  100. 100 int main()
  101. 101 {
  102. 102 Get_int(N);
  103. 103 for(int i=1;i<=N;i++)
  104. 104 {
  105. 105 printf("Case #%d: ",i);
  106. 106 Clean();
  107. 107 Read_Build();
  108. 108 Dijkstra(S);
  109. 109 Print();
  110. 110 }
  111. 111 return 0;
  112. 112 }

[题解]UVA10986 Sending email的更多相关文章

  1. uva 10986 - Sending email(最短路Dijkstra)

    题目连接:10986 - Sending email 题目大意:给出n,m,s,t,n表示有n个点,m表示有m条边,然后给出m行数据表示m条边,每条边的数据有连接两点的序号以及该边的权值,问说从点s到 ...

  2. Sending e-mail with Spring MVC---reference

    reference from:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of con ...

  3. Sending e-mail with Spring MVC--转载

    原文地址:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of contents: 1.S ...

  4. Sending Email from mailx Command in Linux Using Gmail’s SMTP

    The mailx or mail command in Linux is still providing service for guys like me, especially when we n ...

  5. Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference

    Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify ...

  6. Sending e-mail

    E-mail functionality uses the Apache Commons Email library under the hood. You can use theplay.libs. ...

  7. Sending Email In .NET Core 2.0

    Consider the following written in .NET Core 2.0. SmtpClient client = ) { UseDefaultCredentials = tru ...

  8. UVA 10896 Sending Email

    这个题目真是伤透脑筋了,一直RE,连着改了好几个版本,又是spfa,又是单调队列dijkstra+单调队列,总是不过,后来发现M开小了,双向边应该开m的两倍,悲剧啊!!!以后不管怎样,数组一定要尽量开 ...

  9. UVa 10986 - Sending email

    题目大意:网络中有n个SMTP服务器,有m条电缆将它们相连,每条电缆传输信息需要一定的时间.现在给出信息的起点和终点,计算所需的最小时间. 有权图上的单源最短路问题(Single-Source Sho ...

随机推荐

  1. ecos matlab版本安装

    官网链接 github地址 1.注意不仅要下载matlab版本,同时还要下载c版本,因为matlab版本缺少第三方软件,将两个版本解压缩后将c版本下的文件夹external,ecos_bb,inclu ...

  2. 三张图秒懂Redis集群设计原理

    转载Redis Cluster原理 转载https://blog.csdn.net/yejingtao703/article/details/78484151 redis集群部署方式: 单机 主从 r ...

  3. vue学习11-监听属性

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta http ...

  4. golang中的反射reflect详解

    先重复一遍反射三定律: 1.反射可以将"接口类型变量"转换为"反射类型对象". 2.反射可以将"反射类型对象"转换为"接口类型变量 ...

  5. golang中往脚本传递参数的两种用法os.Args和flag

    1. os.Args package main import ( "fmt" "os" ) func main() { // 执行:./demo.exe 127 ...

  6. CSS基本语法(三)

    目录 CSS基础语法(三) 十五.CSS定位 1.为什么要使用定位 2.定位组成 定位模式 静态定位 相对定位 绝对定位** 固定定位 粘性定位 边偏移 子绝父相 3.定位的叠放次序 4.拓展 绝对定 ...

  7. java 方法实例

    // 方法 public class Demo { public static void main(String[] args) { m(); m2(2); m3('3', 4); m4(4, 6); ...

  8. html+css+js(登录页)

    直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. javascript 判断对像是否相等

    在Javascript中相等运算包括"==","==="全等,两者不同之处,不必多数,本篇文章我们将来讲述如何判断两个对象是否相等? 你可能会认为,如果两个对象 ...

  10. vue中router与route区别

    1.$route对象 $route对象表示当前的路由信息,包含了当前 URL 解析得到的信息.包含当前的路径,参数,query对象等. 1.    $route.path      字符串,对应当前路 ...