单源最短路

Invitation Cards  http://poj.org/problem?id=1511

dij+priority queue  o (elogv)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4 #define mt(a,b) memset(a,b,sizeof(a))
  5 using namespace std;
  6 typedef __int64 LL;
  7 const int inf=0x3f3f3f3f;
  8 class Dijkstra { ///单源最短路 o(ME*log(MV))
  9     typedef int typec;///边权的类型
 10     static const int ME=1000010;///边的个数
 11     static const int MV=1000010;///点的个数
 12     struct Q {
 13         int id;
 14         typec w;
 15         friend bool operator <(const Q &a,const Q &b) {
 16             return a.w>b.w;
 17         }
 18     } now;
 19     priority_queue<Q> q;
 20     struct E {
 21         int v,next;
 22         typec w;
 23     } e[ME];
 24     int le,head[MV];
 25     typec dist[MV];
 26     bool used[MV];
 27 public:
 28     void init() {
 29         le=0;
 30         mt(head,-1);
 31     }
 32     void add(int u,int v,typec w) {
 33         e[le].v=v;
 34         e[le].w=w;
 35         e[le].next=head[u];
 36         head[u]=le++;
 37     }
 38     void solve(int s) {
 39         for(int i=0; i<MV; i++) {
 40             used[i]=true;
 41             dist[i]=inf;
 42         }
 43         dist[s]=0;
 44         now.id=s;
 45         now.w=0;
 46         while(!q.empty()) q.pop();
 47         q.push(now);
 48         while(!q.empty()) {
 49             now=q.top();
 50             q.pop();
 51             int u=now.id;
 52             if(used[u]) {
 53                 used[u]=false;
 54                 for(int i=head[u]; ~i; i=e[i].next) {
 55                     int v=e[i].v;
 56                     typec w=e[i].w;
 57                     if(used[v]&&dist[v]>w+dist[u]) {
 58                         dist[v]=w+dist[u];
 59                         now.id=v;
 60                         now.w=dist[v];
 61                         q.push(now);
 62                     }
 63                 }
 64             }
 65         }
 66     }
 67     typec getdist(int id) {
 68         return dist[id];
 69     }
 70 } gx;
 71 struct IN{
 72     int u,v,w;
 73 }e[1000010];
 74 int main() {
 75     int t,n,m,u,v,w;
 76     while(~scanf("%d",&t)) {
 77         while(t--) {
 78             scanf("%d%d",&n,&m);
 79             gx.init();
 80             for(int i=0;i<m;i++) {
 81                 scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
 82                 gx.add(e[i].u,e[i].v,e[i].w);
 83             }
 84             gx.solve(1);
 85             LL ans=0;
 86             for(int i=1; i<=n; i++) {
 87                 ans+=gx.getdist(i);
 88             }
 89             gx.init();
 90             for(int i=0;i<m;i++){
 91                 gx.add(e[i].v,e[i].u,e[i].w);
 92             }
 93             gx.solve(1);
 94             for(int i=1; i<=n; i++) {
 95                 ans+=gx.getdist(i);
 96             }
 97             printf("%I64d\n",ans);
 98         }
 99     }
100     return 0;
101 }

MPI Maelstrom http://poj.org/problem?id=1502

dij+priority queue  o (elogv)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4 #include<stack>
  5 #include<algorithm>
  6 #define mt(a,b) memset(a,b,sizeof(a))
  7 using namespace std;
  8 typedef __int64 LL;
  9 const int inf=0x3f3f3f3f;
 10 class Dijkstra { ///单源最短路 o(ME*log(MV))
 11     typedef int typec;///边权的类型
 12     static const int ME=10010;///边的个数
 13     static const int MV=110;///点的个数
 14     struct Q {
 15         int id;
 16         typec w;
 17         friend bool operator <(const Q &a,const Q &b) {
 18             return a.w>b.w;
 19         }
 20     } now;
 21     priority_queue<Q> q;
 22     struct E {
 23         int v,next;
 24         typec w;
 25     } e[ME];
 26     int le,head[MV];
 27     typec dist[MV];
 28     bool used[MV];
 29 public:
 30     void init() {
 31         le=0;
 32         mt(head,-1);
 33     }
 34     void add(int u,int v,typec w) {
 35         e[le].v=v;
 36         e[le].w=w;
 37         e[le].next=head[u];
 38         head[u]=le++;
 39     }
 40     void solve(int s) {
 41         for(int i=0; i<MV; i++) {
 42             used[i]=true;
 43             dist[i]=inf;
 44         }
 45         dist[s]=0;
 46         now.id=s;
 47         now.w=0;
 48         while(!q.empty()) q.pop();
 49         q.push(now);
 50         while(!q.empty()) {
 51             now=q.top();
 52             q.pop();
 53             int u=now.id;
 54             if(used[u]) {
 55                 used[u]=false;
 56                 for(int i=head[u]; ~i; i=e[i].next) {
 57                     int v=e[i].v;
 58                     typec w=e[i].w;
 59                     if(used[v]&&dist[v]>w+dist[u]) {
 60                         dist[v]=w+dist[u];
 61                         now.id=v;
 62                         now.w=dist[v];
 63                         q.push(now);
 64                     }
 65                 }
 66             }
 67         }
 68     }
 69     typec getdist(int id) {
 70         return dist[id];
 71     }
 72 } gx;
 73 int main(){
 74     int n;
 75     char tmp[32];
 76     while(~scanf("%d",&n)){
 77         gx.init();
 78         for(int i=2;i<=n;i++){
 79             for(int j=1;j<i;j++){
 80                 scanf("%s",tmp);
 81                 if(tmp[0]==‘x‘){
 82                     continue;
 83                 }
 84                 int sum=0;
 85                 for(int k=0;tmp[k];k++){
 86                     sum*=10;
 87                     sum+=tmp[k]-‘0‘;
 88                 }
 89                 gx.add(i,j,sum);
 90                 gx.add(j,i,sum);
 91             }
 92         }
 93         gx.solve(1);
 94         int ans=0;
 95         for(int i=1;i<=n;i++){
 96             ans=max(ans,gx.getdist(i));
 97         }
 98         printf("%d\n",ans);
 99     }
100     return 0;
101 }

Tram http://poj.org/problem?id=1847

dij+priority queue  o (elogv)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<stack>
 5 #include<algorithm>
 6 #define mt(a,b) memset(a,b,sizeof(a))
 7 using namespace std;
 8 typedef __int64 LL;
 9 const int inf=0x3f3f3f3f;
10 class Dijkstra { ///单源最短路 o(ME*log(MV))
11     typedef int typec;///边权的类型
12     static const int ME=10010;///边的个数
13     static const int MV=110;///点的个数
14     struct Q {
15         int id;
16         typec w;
17         friend bool operator <(const Q &a,const Q &b) {
18             return a.w>b.w;
19         }
20     } now;
21     priority_queue<Q> q;
22     struct E {
23         int v,next;
24         typec w;
25     } e[ME];
26     int le,head[MV];
27     typec dist[MV];
28     bool used[MV];
29 public:
30     void init() {
31         le=0;
32         mt(head,-1);
33     }
34     void add(int u,int v,typec w) {
35         e[le].v=v;
36         e[le].w=w;
37         e[le].next=head[u];
38         head[u]=le++;
39     }
40     void solve(int s) {
41         for(int i=0; i<MV; i++) {
42             used[i]=true;
43             dist[i]=inf;
44         }
45         dist[s]=0;
46         now.id=s;
47         now.w=0;
48         while(!q.empty()) q.pop();
49         q.push(now);
50         while(!q.empty()) {
51             now=q.top();
52             q.pop();
53             int u=now.id;
54             if(used[u]) {
55                 used[u]=false;
56                 for(int i=head[u]; ~i; i=e[i].next) {
57                     int v=e[i].v;
58                     typec w=e[i].w;
59                     if(used[v]&&dist[v]>w+dist[u]) {
60                         dist[v]=w+dist[u];
61                         now.id=v;
62                         now.w=dist[v];
63                         q.push(now);
64                     }
65                 }
66             }
67         }
68     }
69     typec getdist(int id) {
70         return dist[id];
71     }
72 } gx;
73 int main(){
74     int n,a,b;
75     while(~scanf("%d%d%d",&n,&a,&b)){
76         gx.init();
77         for(int i=1;i<=n;i++){
78             int m,to;
79             scanf("%d",&m);
80             for(int j=0;j<m;j++){
81                 scanf("%d",&to);
82                 if(j!=0)    gx.add(i,to,1);
83                 else        gx.add(i,to,0);
84             }
85         }
86         gx.solve(a);
87         int ans=gx.getdist(b);
88         if(ans>1024) ans=-1;
89         printf("%d\n",ans);
90     }
91     return 0;
92 }

Til the Cows Come Home http://poj.org/problem?id=2387

dij+priority queue  o (elogv)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<stack>
 5 #include<algorithm>
 6 #define mt(a,b) memset(a,b,sizeof(a))
 7 using namespace std;
 8 typedef __int64 LL;
 9 const int inf=0x3f3f3f3f;
10 class Dijkstra { ///单源最短路 o(ME*log(MV))
11     typedef int typec;///边权的类型
12     static const int ME=1000010;///边的个数
13     static const int MV=1000010;///点的个数
14     struct Q {
15         int id;
16         typec w;
17         friend bool operator <(const Q &a,const Q &b) {
18             return a.w>b.w;
19         }
20     } now;
21     priority_queue<Q> q;
22     struct E {
23         int v,next;
24         typec w;
25     } e[ME];
26     int le,head[MV];
27     typec dist[MV];
28     bool used[MV];
29 public:
30     void init() {
31         le=0;
32         mt(head,-1);
33     }
34     void add(int u,int v,typec w) {
35         e[le].v=v;
36         e[le].w=w;
37         e[le].next=head[u];
38         head[u]=le++;
39     }
40     void solve(int s) {
41         for(int i=0; i<MV; i++) {
42             used[i]=true;
43             dist[i]=inf;
44         }
45         dist[s]=0;
46         now.id=s;
47         now.w=0;
48         while(!q.empty()) q.pop();
49         q.push(now);
50         while(!q.empty()) {
51             now=q.top();
52             q.pop();
53             int u=now.id;
54             if(used[u]) {
55                 used[u]=false;
56                 for(int i=head[u]; ~i; i=e[i].next) {
57                     int v=e[i].v;
58                     typec w=e[i].w;
59                     if(used[v]&&dist[v]>w+dist[u]) {
60                         dist[v]=w+dist[u];
61                         now.id=v;
62                         now.w=dist[v];
63                         q.push(now);
64                     }
65                 }
66             }
67         }
68     }
69     typec getdist(int id) {
70         return dist[id];
71     }
72 } gx;
73 int main(){
74     int m,n,u,v,w;
75     while(~scanf("%d%d",&m,&n)){
76         gx.init();
77         while(m--){
78             scanf("%d%d%d",&u,&v,&w);
79             gx.add(u,v,w);
80             gx.add(v,u,w);
81         }
82         gx.solve(1);
83         printf("%d\n",gx.getdist(n));
84     }
85     return 0;
86 }

Subway http://poj.org/problem?id=2502

dij+priority queue  o (elogv)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<queue>
  5 #include<stack>
  6 #include<algorithm>
  7 #define mt(a,b) memset(a,b,sizeof(a))
  8 using namespace std;
  9 typedef __int64 LL;
 10 const int inf=0x3f3f3f3f;
 11 const int M=210;
 12 class Dijkstra { ///单源最短路 o(ME*log(MV))
 13     typedef double typec;///边权的类型
 14     static const int ME=M*M;///边的个数
 15     static const int MV=M;///点的个数
 16     struct Q {
 17         int id;
 18         typec w;
 19         friend bool operator <(const Q &a,const Q &b) {
 20             return a.w>b.w;
 21         }
 22     } now;
 23     priority_queue<Q> q;
 24     struct E {
 25         int v,next;
 26         typec w;
 27     } e[ME];
 28     int le,head[MV];
 29     typec dist[MV];
 30     bool used[MV];
 31 public:
 32     void init() {
 33         le=0;
 34         mt(head,-1);
 35     }
 36     void add(int u,int v,typec w) {
 37         e[le].v=v;
 38         e[le].w=w;
 39         e[le].next=head[u];
 40         head[u]=le++;
 41     }
 42     void solve(int s) {
 43         for(int i=0; i<MV; i++) {
 44             used[i]=true;
 45             dist[i]=inf;
 46         }
 47         dist[s]=0;
 48         now.id=s;
 49         now.w=0;
 50         while(!q.empty()) q.pop();
 51         q.push(now);
 52         while(!q.empty()) {
 53             now=q.top();
 54             q.pop();
 55             int u=now.id;
 56             if(used[u]) {
 57                 used[u]=false;
 58                 for(int i=head[u]; ~i; i=e[i].next) {
 59                     int v=e[i].v;
 60                     typec w=e[i].w;
 61                     if(used[v]&&dist[v]>w+dist[u]) {
 62                         dist[v]=w+dist[u];
 63                         now.id=v;
 64                         now.w=dist[v];
 65                         q.push(now);
 66                     }
 67                 }
 68             }
 69         }
 70     }
 71     typec getdist(int id) {
 72         return dist[id];
 73     }
 74 } gx;
 75 struct point{
 76     double x,y;
 77 }p[M];
 78 double a[M][M];
 79 int main() {
 80     int i,j,c,flag=0;
 81     scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y);
 82     int n=2;
 83     while(~scanf("%lf%lf",&p[n].x,&p[n].y)) {
 84         if(p[n].x==-1&&p[n].y==-1) {
 85             flag=0;
 86             continue;
 87         }
 88         if(flag) {
 89             a[n][n-1]=sqrt((p[n].x-p[n-1].x)*(p[n].x-p[n-1].x)+(p[n].y-p[n-1].y)*(p[n].y-p[n-1].y))/40000.0;
 90             a[n-1][n]=a[n][n-1];
 91         }
 92         n++;
 93         flag=1;
 94     }
 95     for(i=0; i<n; i++) {
 96         for(j=0; j<n; j++) {
 97             if(i!=j&&a[i][j]==0.0) {
 98                 a[i][j]=sqrt((p[j].x-p[i].x)*(p[j].x-p[i].x)+(p[j].y-p[i].y)*(p[j].y-p[i].y))/10000.0;
 99             }
100         }
101     }
102     gx.init();
103     for(i=0;i<n;i++){
104         for(j=0;j<n;j++){
105             gx.add(i,j,a[i][j]);
106         }
107     }
108     gx.solve(0);
109     printf("%0.0f\n",60.0*gx.getdist(1));
110     return 0;
111 }

end

时间: 2024-08-16 03:01:11

单源最短路的相关文章

【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 邻接矩阵实现的单源最短路 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include

Dijkstra算法 --- 单源最短路

Dijkstra算法适用于边权值为正的情况,可用于计算正权图上的单元最短路. 其伪代码如下: 设d[v0] = 0, 其他d[i] = INF 循环n次{ 在所有未标号的结点中,选取d值最小的结点x 给结点x加上永久标号 对于从x出发的所有边,执行松弛操作. } //松弛操作的伪代码如下: RELAX(u,v,w) if(u.d + w(u,v) < v.d){ v.d = w.d + w(u,v); pre[v] = u; } Dijkstra算法代码: /* Dijkstra 单源最短路算法

常见模板(欧拉筛素数,最小生成树,快排,并查集,单源最短路)

欧拉筛素数: #include<cstdio> #define maxn 10000000+10 using namespace std; int n,prime[5000001],num_prime=0,m; bool if_prime[maxn]; void euler(int limit) { for(int i=2;i<=limit;i++) { if(!if_prime[i]) prime[++num_prime]=i; for(int j=1;prime[j]*i<=l

利用分支限界法求解单源最短路(Dijkstra)问题

分支限界法定义:采用BFS算法,并使用剪枝函数的算法称为分支界限法. 分支限界法解释:按广度优先的原则,有选择的在其child中进行扩展,从而舍弃不含有最优解的分支,不断重复这一过程,直到找到答案或者判定无解. 分支界限法常常用到优先队列来选择最佳扩展节点,有时也会用到普通队列,以先进先出为原则来进行筛选. 单源最短路问题定义:给定有向图和起点,寻找到达所有点的最短路径. 单源最短路的分支限界法概述:首先把节点加入优先队列,之后不断地从队列中取出最优扩展点,观察其可抵达的所有目标节点,若当前路径

UVA 658 It&#39;s not a Bug, it&#39;s a Feature! (单源最短路,dijkstra+优先队列,变形,经典)

题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了:要打多久才能无bug?(同1补丁可重复打) 分析: n<=20,那么用位来表示bug的话有220=100万多一点.不用建图了,图实在太大了,用位图又不好玩.那么直接用隐式图搜索(在任意点,只要满足转移条件,任何状态都能转). 但是有没有可能每个状态都要搜1次啊?那可能是100万*100万啊,这样出题

单源最短路_SPFA_C++

当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模板:http://www.cnblogs.com/hadilo/p/5934679.html 我感觉自己讲的不会很好,丢一个链接算了 算法详解:http://www.360doc.com/content/13/1208/22/14357424_335569176.shtml 伪代码是自己写的: 可以

HDU-4849 Wow! Such City! (单源最短路)

Problem Description Doge, tired of being a popular image on internet, is considering moving to another city for a new way of life. In his country there are N (2 ≤N≤ 1000) cities labeled 0 . . . N - 1. He is currently in city 0. Meanwhile, for each pa

【裸单源最短路:Dijkstra算法两种版本】hdu 1874 畅通工程续

Source : hdu 1874 畅通工程续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰. 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. Input 本题目包含多组数据,请处理到文件结束.

再看最短路算法 1 —— 单源最短路

学了多年的算法,最短路问题相当之常见———— 好久没写过最短路的问题了,直到昨天闲的无聊来了一题——BZOJ3402(HansBug:额才发现我弱到只能刷水的地步了TT) 一看这不是明显的单源最短路么呵呵...于是直接上来来了个dijkstra,而且用的是邻接表存储图—— Submit之后,结果却是—— 我立刻被雷到了QAQ...于是立刻改写spfa,结果—— 4000ms+(估计还不止)和192ms究竟是怎样的差距啊QAQ,本人虽然早都听说过spfa的强大性,但是未曾想过差距会如此可怕,于是H

单源最短路 判负环

Wormholes http://poj.org/problem?id=3259 spfa 2e 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7 const int inf=0x3f3f3f3f; 8 const int