Problem Description

某省自从实行了非常多年的畅通project计划后。最终修建了非常多路。只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择。而某些方案要比还有一些方案行走的距离要短非常多。这让行人非常困扰。

如今,已知起点和终点。请你计算出要从起点到终点,最短须要行走多少距离。

Input

本题目包括多组数据,请处理到文件结束。

每组数据第一行包括两个正整数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),分别代表起点和终点。

Output

对于每组数据,请在一行里输出最短须要行走的距离。假设不存在从S到T的路线,就输出-1.

Sample Input

3 3

0 1 1

0 2 3

1 2 1

0 2

3 1

0 1 1

1 2

Sample Output

2

-1

Author

linle

Source

2008浙大研究生复试热身赛(2)——全真模拟

起点到终点的最短路 这里给出3种算法。Floyd。dijkstra和spfa

从提交的代码得速度来看。dijkstra>spfa>Floyd

可是别人都说spfa最快。。。(我就不知道了)

folyd不推荐使用。由于他最慢。,数据大一点的话更不行。

。(只是他最简单)

(1)Floyd

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<28)
using namespace std; int cost[205][205];
bool used[205];
int n,m;
int d[205];
void floyd()
{
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int i,j;
for(i=0;i<n;i++)
{
cost[i][i]=0;
for(j=0;j<n;j++)
{
if(i!=j) cost[i][j]=INF;
}
}
int a,b,value,start,endl;
for(i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&value);
if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。 }
scanf("%d %d",&start,&endl);
floyd();
if(cost[start][endl]!=INF) printf("%d\n",cost[start][endl]);
else printf("-1\n");
}
return 0;
}

2:dijkstra

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<28)
using namespace std; int cost[205][205];
bool used[205];
int n,m;
int d[205];
void dijkstra(int s)
{
fill(d,d+n,INF);
fill(used,used+n,false);
d[s]=0;
while(true)
{
int v=-1;
for(int u=0;u<n;u++)
{
if(!used[u]&&(v==-1||d[u]<d[v])) v=u;
}
if(v==-1) break;
used[v]=true;
for(int u=0;u<n;u++)
{
d[u]=min(d[u],d[v]+cost[v][u]);
}
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int i,j;
for(i=0;i<n;i++)
{
cost[i][i]=0;
for(j=0;j<n;j++)
{
if(i!=j) cost[i][j]=INF;
}
}
int a,b,value,start,endl;
for(i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&value);
if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。 }
scanf("%d %d",&start,&endl);
dijkstra(start);
if(d[endl]!=INF) printf("%d\n",d[endl]);
else printf("-1\n");
}
return 0;
}

3:spfa

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<28)
using namespace std; int cost[205][205];
bool used[205];
int n,m;
int d[205];
void SPFA(int src,int des)
{
int i;
for(i=0;i<n;i++) d[i]=INF;
memset(used,false,sizeof(used)); queue<int> myqueue;
while(!myqueue.empty()) myqueue.pop();//清空队列 d[src]=0;
used[src]=1;
myqueue.push(src);
int tmp;
while(!myqueue.empty())
{
tmp=myqueue.front();
myqueue.pop();
used[tmp]=0;
for(i=0;i<n;i++)
if(d[i]>d[tmp]+cost[tmp][i])
{
d[i]=d[tmp]+cost[tmp][i];
if(!used[i])
{
used[i]=1;
myqueue.push(i);
}
}
}
} int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int i,j;
for(i=0;i<n;i++)
{
cost[i][i]=0;
for(j=0;j<n;j++)
{
if(i!=j) cost[i][j]=INF;
}
}
int a,b,value,start,endl;
for(i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&value);
if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。
}
scanf("%d %d",&start,&endl);
SPFA(start,endl);
if(d[endl]!=INF) printf("%d\n",d[endl]);
else printf("-1\n");
}
return 0;
}

hdu1874 畅通project续 最短路 floyd或dijkstra或spfa的更多相关文章

  1. HDU1874畅通project续 dijkstra&amp;&amp;floyd

    畅通project续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Time Limit: 3000/1000 MS (Java/Others)    ...

  2. hdu1874 畅通project续(求最短路径)

    畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. HDU 1874 畅通project续 (最短路径)

    畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. hdoj 1874 畅通project续【SPFA】

    畅通project续 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  5. HDOJ 1874 畅通project续

    畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. hdu1874畅通工程续(floyd)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. HDU1874畅通工程续(floyd||dijkstra)

    看了看floyd和dijkstra,然后就找了两个练习来捉 #include<iostream> #include<stdio.h> #include<string.h& ...

  8. HDU-1874 畅通工程续 (最短路径启蒙题)

    hdu 1874比较基础,拿来练各种刚学会的算法比较好,可以避免好多陷阱,典型的最短路模板题 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memor ...

  9. hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...

随机推荐

  1. PHP DES-ECB加密对接Java解密

    最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一 ...

  2. 用例子看ASP.NET Core Identity是什么?

    原文:用例子看ASP.NET Core Identity是什么? 目录 前言 基于声明的认证(Claims-based Authentication) Claim 在ASP.NET Core Iden ...

  3. vue2.0-transition配合animate.css

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Create the Data Access Layer

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspn ...

  5. js中arguments对象和this对象

    js中arguments对象和this属性 如果不注重复习,花时间准备的材料毫无意义 arguments对象和this对象都是对象 直接来代码 <!DOCTYPE html> <ht ...

  6. assert 的理解

    assert 可以实现如下功能: 保证参数之间的大小等约束关系: 函数执行过程中得到的中间结果是否符合预期: def gen_batch(batch_size, skip_window, num_sk ...

  7. linux关于用户密码家目录总结

    创建用户及其家目录useradd -d /home/tomcat -m tomcat接着修改密码passwd tomcat usermod -s /sbin/nologin + 用户名 禁止登录ssh ...

  8. Gym - 100203I I WIN 网络流

    Gym - 100203I  I WIN 题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN. 思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下 ...

  9. 51nod 最大子矩阵和

    一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 我们可以降维,枚举矩形的长,然后算出一个一维数组,然后就转化成了最大字段和问题 #include< ...

  10. android与C# WebService基于ksoap通信(C#篇)

    1.打开VS 2013新建项目>>ASP.NET空WEB应用程序(我用的是.net 4.0) 2.在刚建立的项目上加入新建项(WebService) 这时TestService的代码例如以 ...