Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

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

 
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
 
 dijkstra代码:
 #include <stdio.h>
#define N 210
#define INF 100000000
int n, m;
int vis[N], dis[N], cost[N][N];
int min(int x, int y)
{
return x < y ? x : y;
}
void dijkstra(int s, int t)
{
int u, v;
for(u = ; u < n; u++)
{
vis[u] = ;
dis[u] = INF;
}
dis[s] = ;
while(true)
{
v = -;
for(u = ; u < n; u++)
if(!vis[u] && (v == - || dis[u]<dis[v]))
v = u;
if(v == -)
break;
vis[v] = ;
for(u = ; u < n; u++)
dis[u] = min(dis[u], dis[v]+cost[v][u]);
if(v == t)
break;
}
if(dis[t] == INF)
printf("-1\n");
else
printf("%d\n", dis[t]);
return ;
}
int main()
{
int s, t;
while(~scanf("%d%d", &n, &m))
{
//if(n<0 || n>=200 || m<0 || m>= 1000)
// break;
int i, j, a, b, c;
for(i = ; i < n; i++)
for(j = ; j < n; j++)
cost[i][j] = INF;
while(m --)
{
scanf("%d%d%d", &a, &b, &c);
if(cost[a][b] > c)
cost[a][b] = cost[b][a] = c;
}
scanf("%d%d", &s, &t);
dijkstra(s, t);
}
return ;
}

spfa代码:

 #include <stdio.h>
#include <queue>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
int n, m, cnt, t;
int vis[], used[], dis[];
int head[];
struct node
{
int from, to, val, next;
}edge[];
int add(int x, int y, int z)
{
edge[cnt].from = x;
edge[cnt].to = y;
edge[cnt].val = z;
edge[cnt].next = head[x];
head[x] = cnt++;
}
void spfa(int s)
{
priority_queue <int> q;
memset(vis, , sizeof(vis));
memset(dis, INF, sizeof(dis));
memset(used, , sizeof(used));
vis[s] = ;
dis[s] = ;
q.push(s);
while(!q.empty())
{
int x = q.top();
q.pop();
vis[x] = ;
for(int i = head[x]; i != -; i = edge[i].next)
{
int y = edge[i].to;
if(dis[y] > dis[x]+edge[i].val)
{
dis[y] = dis[x]+edge[i].val;
q.push(y);
vis[y] = ;
used[y]++;
if(used[y] > n)
return ;
}
}
}
if(q.empty())
{
if(dis[t]==INF)
printf("-1\n");
else
printf("%d\n", dis[t]);
}
return ;
}
int main()
{
int s, a, b, x;
while(~scanf("%d%d", &n, &m))
{
cnt = ;
memset(head, -, sizeof(head));
while(m--)
{
scanf("%d%d%d", &a, &b, &x);
add(a, b, x);
add(b, a, x);
}
scanf("%d%d", &s, &t);
spfa(s);
}
return ;
}

floyd算法:

 #include <stdio.h>
#define INF 0x3f3f3f3f
#define N 210
int dis[N][N];
int n, m;
void init()
{
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
if(j == i)
dis[i][j] = ;
else
dis[i][j] = INF;
}
void floyd()
{
for(int k = ; k < n; k++)
for(int i = ; i < n; i++)
{
if(dis[i][k] != INF)
{
for(int j = ; j < n; j++)
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
int main()
{
int a, b, x;
while(~scanf("%d%d", &n, &m))
{
init();
while(m--)
{
scanf("%d%d%d", &a, &b, &x);
if(dis[a][b] > x)
dis[a][b] = dis[b][a] = x;
}
floyd();
int s, t;
scanf("%d%d", &s, &t);
if(dis[s][t] != INF)
printf("%d\n", dis[s][t]);
else
printf("-1\n");
}
return ;
}

hdoj 1874 畅通工程续的更多相关文章

  1. hdoj 1874 畅通工程续【dijkstra算法or spfa算法】

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

  2. hdoj 1874 畅通工程续(单源最短路+dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 思路分析:该问题给定一个无向图.起始点和终点,要求求出从起始点到终点的最短距离: 使用Dijks ...

  3. ACM: HDU 1874 畅通工程续-Dijkstra算法

    HDU 1874 畅通工程续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  4. hdu 1874 畅通工程续

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...

  5. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  6. HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题

    参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上:  迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...

  7. hdu 1874 畅通工程续 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...

  8. HDU 1874 畅通工程续【Floyd算法实现】

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

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

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

随机推荐

  1. 设置ajax 同步执行

    Ajax请求默认的都是异步的如果想同步 async设置为false就可以(默认是true) var html = $.ajax({  url: "some.php",  async ...

  2. Win32编程:窗口类样式+窗口外观样式+窗口显示样式

    1.窗口类样式WNDCLASS.style CS_VREDRAW 提供窗口位置变化事件和高度变化事件的处理程序,功能是重绘窗口 CS_HREDRAW 提供窗口位置变化事件和宽度变化事件的处理程序,功能 ...

  3. (转)winform Form 淡入淡出效果

    原文地址:http://blog.csdn.net/a237428367/article/details/5933565 using System.Runtime.InteropServices; p ...

  4. 北京市小升初 zz

    发信人: django (牛魔王), 信区: SchoolEstate 标  题: 北京市小升初掐尖方式的演变过程(看后恍然大悟) 发信站: 水木社区 (Thu Feb  4 10:51:23 201 ...

  5. Tomcat从内存、并发、缓存方面优化方法

    Tomcat有很多方面,从内存.并发.缓存四个方面介绍优化方法.   一.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 cata ...

  6. PIC32MZ tutorial -- Key Debounce

    Today I accomplish a application on PIC32MZ EC Starter Kit. The feature of application is to light u ...

  7. SQL Server 磁盘空间告急(磁盘扩容)转载

    一.背景 在线上系统中,如果我们发现存放数据库文件的磁盘空间不够,我们应该怎么办呢?新买一个硬盘挂载上去可以嘛?(linux下可以直接挂载硬盘进行扩容),但是我们的SQL Server是运行在Wind ...

  8. Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)

    最近公司在给我们培训,主要是网页自动化测试的,现在的工作每天都是测APP,刚刚入门,不过,当我看了别人写的bug之后,就觉得不会觉得能够发现bug多么多么的厉害了. 前两周的时间一直在搭建自动化测试的 ...

  9. Windows7 IE10运行不了JavaScript的问题

    如题,我的环境是Windows7 + IE10,JavaScript怎么也运行不了.郁闷了好一段时间. 后来发现一种办法终于可以让JavaScript运行起来. 具体:  点击 [工具] => ...

  10. VC++ 控制外部程序,向外部程序发送一个消息的方法

    这里需要考虑两部分的内容: 发送端: 查找对应的窗体,找到CWnd的值 向窗体发送消息 举例: CWnd* wnd = FindWindow(NULL, _T("选择题做题过程中" ...