hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)
畅通工程续
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 49963 Accepted Submission(s): 18624
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
- //迪杰斯特拉
- #include<queue>
- #include<stack>
- #include<math.h>
- #include<stdio.h>
- #include<numeric>//STL数值算法头文件
- #include<stdlib.h>
- #include<string.h>
- #include<iostream>
- #include<algorithm>
- #include<functional>//模板类头文件
- using namespace std;
- const int INF=1e9+7;
- const int maxn=100+10;
- int n,m,st,ed;
- int par[maxn];
- int vis[maxn];
- int tu[maxn][maxn];
- int dijkstra(int st,int ed)
- {
- int i,j,k,minn;
- memset(vis,0,sizeof(vis));
- for(i=0; i<n; i++)
- par[i]=tu[st][i];
- vis[st]=1;
- par[st]=0;
- for(i=1; i<n; i++)
- {
- minn=INF;
- for(j=0; j<n; j++)
- {
- if(!vis[j]&&minn>par[j])
- {
- k=j;
- minn=par[j];
- }
- }
- vis[k]=1;
- for(j=0; j<n; j++)
- {
- if(!vis[j]&&par[j]>par[k]+tu[k][j])
- {
- par[j]=par[k]+tu[k][j];
- }
- }
- }
- }
- int main()
- {
- while(~scanf("%d %d",&n,&m))
- {
- int i,j,u,v,w;
- memset(tu,0,sizeof(tu));
- for(i=0; i<n; i++)
- {
- for(j=0; j<n; j++)
- {
- tu[i][j]=i==j?0:INF;
- }
- }
- while(m--)
- {
- scanf("%d %d %d",&u,&v,&w);
- if(tu[u][v]>w)
- tu[u][v]=tu[v][u]=w;
- }
- scanf("%d %d",&st,&ed);
- dijkstra(st,ed);
- printf("%d\n",par[ed]==INF?-1:par[ed]);
- }
- return 0;
- }
- //Floyd弗洛伊德
- #include <cstdio>
- #include <cstring>
- using namespace std;
- #define INF 10000
- const int maxn = 201;
- int tu[maxn][maxn];
- int main()
- {
- int n, m;
- while (scanf("%d%d", &n, &m) != EOF)
- {
- memset (tu, INF, sizeof(tu));
- int u, v, w;
- for (int i = 0; i < m; i++)
- {
- scanf("%d%d%d", &u, &v, &w);
- if (tu[u][v] > w)
- tu[u][v] = tu[v][u] = w;
- }
- for (int i = 0; i < n ; i++)
- tu[i][i] = 0;
- for (int k = 0; k < n; k++)
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- {
- if (tu[i][k] + tu[k][j] < tu[i][j])
- tu[i][j] = tu[j][i] = tu[i][k] + tu[k][j];
- }
- scanf("%d%d", &u, &v);
- if (tu[u][v] < INF)
- printf("%d\n", tu[u][v]);
- else
- printf("-1\n");
- }
- return 0;
- }
//#include<queue> //#include<stack> //#include<vector> //#include<math.h> //#include<stdio.h> //#include<numeric>//STL数值算法头文件 //#include<stdlib.h> //#include<string.h> //#include<iostream> //#include<algorithm> //#include<functional>//模板类头文件 //using namespace std; // //const int INF=1e9+7; //const int maxn=205; //vector<pair<int,int> >E[maxn]; //int d[maxn],inq[maxn];//inq数组表示是否在队列中,d数组表示起点到当前点的距离 // //void init() //{ // for(int i=0; i<maxn; i++) E[i].clear(); // for(int i=0; i<maxn; i++) inq[i]=0; // for(int i=0; i<maxn; i++) d[i]=INF; //} // //int n,m; //int main() //{ // while(cin>>n>>m) // { // int i,j; // init(); // for(i=0; i<m; i++) // { // int x,y,z; // scanf("%d %d %d",&x,&y,&z); // E[x].push_back(make_pair(y,z)); // E[y].push_back(make_pair(x,z)); // } // int s,t; // scanf("%d %d",&s,&t); // queue<int>q; // q.push(s),d[s]=0,inq[s]=1; // while(!q.empty()) // { // int now=q.front(); // q.pop(); // inq[now]=0; // for(int i=0; i<E[now].size(); i++) // { // int v=E[now][i].first; // if(d[v]>d[now]+E[now][i].second) // { // d[v]=d[now]+E[now][i].second; // if(inq[v]) continue; // inq[v]=1; // q.push(v); // } // } // } // if(d[t]==INF) printf("-1\n"); // else printf("%d\n",d[t]); // } // return 0;//}
//"单源最短路"迪杰斯特拉,优先队列 //(m+n)*log(n)的复杂度 //通过中间点来松弛源点到其余各顶点的路径 #include<queue> #include<stack> #include<vector> #include<math.h> #include<stdio.h> #include<numeric>//STL数值算法头文件 #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #include<functional>//模板类头文件 using namespace std; const int INF=1e9+7; const int maxn=205; vector<pair<int,int> >E[maxn]; int d[maxn];//d数组表示起点到当前点的距离 void init() { for(int i=0; i<maxn; i++) E[i].clear(); for(int i=0; i<maxn; i++) d[i]=INF;}
int n,m; int main() { while(cin>>n>>m) { int i,j; init(); for(i=0; i<m; i++) { int x,y,z; scanf("%d %d %d",&x,&y,&z); E[x].push_back(make_pair(y,z)); E[y].push_back(make_pair(x,z)); } int s,t; scanf("%d %d",&s,&t); priority_queue<pair<int,int> >q; d[s]=0; q.push(make_pair(-d[s],s)); while(!q.empty()) { int now=q.top().second; q.pop(); for(int i=0; i<E[now].size(); i++) { int v=E[now][i].first; if(d[v]>d[now]+E[now][i].second) { d[v]=d[now]+E[now][i].second; q.push(make_pair(-d[v],v)); } } } if(d[t]==INF) printf("-1\n"); else printf("%d\n",d[t]); } return 0;}
//时间复杂度最低
#include<map> #include<queue> #include<stack> #include<vector> #include<math.h> #include<cstdio> #include<sstream> #include<numeric>//STL数值算法头文件 #include<stdlib.h> #include <ctype.h> #include<string.h> #include<iostream> #include<algorithm> #include<functional>//模板类头文件 using namespace std; typedef long long ll; const int maxn=1100; const int INF=0x3f3f3f3f; int n,m,dis[maxn],head[maxn],cnt[maxn],len; bool vis[maxn]; struct edge { int to,val,next; } e[maxn]; void add(int from,int to,int val) { e[len].to=to; e[len].val=val; e[len].next=head[from]; head[from]=len++; } bool spfa(int s,int t) { memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) dis[i]=INF; queue<int> q; q.push(s); cnt[s]++; vis[s]=true; dis[s]=0; while(!q.empty()) { int cur=q.front(); q.pop(); vis[cur]=false; for(int i=head[cur]; i!=-1; i=e[i].next) { int id=e[i].to; if(dis[id] > dis[cur]+e[i].val) { dis[id] = dis[cur] + e[i].val; if(!vis[id]) { cnt[id]++; vis[id]=true; if(cnt[cur]>n) return false; q.push(id); } } } } return true; } int main() { while(~scanf("%d%d",&n,&m)) { len=0; memset(head,-1,sizeof(head)); for(int i=0; i<m; i++) { int from,to,val; scanf("%d%d%d",&from,&to,&val); add(from,to,val); add(to,from,val); } int s,t; scanf("%d%d",&s,&t); spfa(s,t); if(spfa(s,t)&&dis[t]!=INF) printf("%d\n",dis[t]); else printf("-1\n"); } return 0;}
hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)的更多相关文章
- HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题
参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上: 迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
- hdu 1874 畅通工程续
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...
- HDU——1874畅通工程续(邻接矩阵弗洛伊德)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 1874 畅通工程续 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...
- HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...
- HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)
题目链接: 传送门 畅通工程续 Time Limit: 1000MS Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
随机推荐
- 2015/9/29 Python基础(20):类的授权
类的授权 1.包装包装在Python编程世界中时经常会被提到的一个术语.它是一个通用的名字,意思是对一个已存在的对象进行包装,不管它是数据类型,还是一段代码,可以是对一个已存在的对象,增加新的,删除不 ...
- JS中client/offset/scroll等的宽高解析
原文地址:→传送门 window相关宽高属性 1. window.outerHeight (窗口的外层的高度) / window.outerWidth (窗口的外层的宽度) window.outerH ...
- 基本控件文档-UITableView---iOS-Apple苹果官方文档翻译
//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html 技术博客http://www.cnblogs.com/ChenYi ...
- CodeForces 869B
Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...
- html5手机Web单页应用实践--起点移动阅读
一开始以hybrid形式做了一个android的小说阅读客户端,叫4G阅读.而后由于业务需求,要迅速实现纯手机html5 版的,所以就直接在原先客户端内内嵌的网页进行改版,快速实现以后在优化的过程中发 ...
- Friends and Berries URAL - 2067 (计算三点共线和计算的时候的注意点)
题目链接:https://cn.vjudge.net/problem/URAL-2067 具体思路:判断三点共线就可以了,只有一对点能满足,如果一对就没有那就没有满足的. 在计算的时候,要注意,如果是 ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
- 深入理解Spring系列之六:bean初始化
转载 https://mp.weixin.qq.com/s/SmtqoELzBEdZLo8wsSvUdQ <深入理解Spring系列之四:BeanDefinition装载前奏曲>中提到,对 ...
- ogg:Extract 进程遇长事务执行 Forcestop 引发的惨案
http://www.linuxidc.com/Linux/2015-04/115777.htm SQL> select t.addr,t.START_DATE from v$transacti ...
- HDU 6198 2017沈阳网络赛 线形递推
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198 题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几. 解法:先暴力打表看看有没有规律 ...