hdoj 2544最短路
输入保证至少存在1条商店到赛场的路线。
#include<stdio.h>
#define INF 0x3f3f3f3f
#define N 1010
int vis[N], dis[N], cost[N][N];
int n, m, s, w, p, q, t;
int min(int x, int y)
{ return x < y ? x : y;
}
void dijkstra(int beg)
{
int u, v;
for(u = ; u <= n; u++)
{
vis[u] = ;
dis[u] = INF;
}
dis[beg] = ;
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]);
}
}
int main()
{
int i , j;
while(~scanf("%d%d%d", &n, &m, &s))
{
for(i = ; i <= n; i++)
for(j = i; j <= n; j++)
cost[i][j] = cost[j][i] = INF;
while(m--)
{
scanf("%d%d%d", &p, &q, &t);
if(cost[q][p] > t)
cost[q][p] = t;
}
scanf("%d", &w);
int sum = INF, b;
dijkstra(s);
for(i = ; i <= w; i++)
{
scanf("%d", &b);
if(sum > dis[b])
sum = dis[b];
} if(sum == INF)
printf("-1\n");
else
printf("%d\n", sum);
}
return ;
}
spfa代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int n, m, head[];
int used[], vis[], dis[];
struct node
{
int from, to, val, next;
}edge[];
int cnt;
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(dis, INF, sizeof(dis));
memset(used, , sizeof(used));
dis[s] = ;
vis[s] = ;
q.push(s);
used[s]++;
while(!q.empty())
{
int x;
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;
if(!vis[y])
{
q.push(y);
used[y]++;
vis[y] = ;
if(used[y] > n)
{
return ;
}
}
}
}
}
if(q.empty())
printf("%d\n", dis[n]);
return ;
}
int main()
{
while(~scanf("%d%d", &n, &m), n&&m)
{
int k = ;
cnt = ;
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
for(int i = ; i < m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
spfa();
}
return ;
}
hdoj 2544最短路的更多相关文章
- hdoj 2544 最短路
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=2544 分析:Dijkstra算法 //2013-10-30 10:01:25 Accepted 254 ...
- hdoj 2544 最短路【dijkstra or spfa】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdoj 2544 最短路(最短路+Dijkstrea算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路分析:该问题给定一个无向图,要求求从起始点到终点的最短路径长度:可以使用dijkstra算法 ...
- HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- hdu 2544 最短路
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HUD.2544 最短路 (Dijkstra)
HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...
随机推荐
- sysbench压力测试工具简介和使用(一)
sysbench压力测试工具安装和参数介绍 一.sysbench压力测试工具简介: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据 ...
- mybatis配置优化
1.加入日志log4j 1)加入jar包:log4j-1.2.17.jar 2)加入log4j配置文件: 可以使properties或者xml形式 log4j.rootLogger = DEBUG,C ...
- Linux6(5)安装Oracle Rac11g
1.创建用户组.以root身份运行以下命令:/usr/sbin/groupadd -g 501 oinstall /usr/sbin/groupadd -g 502 dba /usr/sbin/gro ...
- [VB.NET]取消按钮按下的默认事件响应
大家应该有过这样的经历:有两个文本框a,b.通过编程,当我们在A中回车后,光标会移动到B文本框. 但是,不可避免的会听到一声“铛”的声音. 解决办法: 在Keydown或者KeyUp事件中,设置e.S ...
- FW: Chef集中管理工具实践之 (1) 环境部署
本文转载:http://heylinux.com/archives/2208.html Chef集中管理工具实践之 (1) 环境部署 目录结构Chef集中管理工具实践之 (0) 什么是ChefChef ...
- 关于view.measure
在编写下啦刷新的项目代码的时候,在Listview的HeaderView中的head.xml文件中,根布局为RelativeLayout的时候,在计算headerView.measure的时候,出现空 ...
- jsp与Servlet
- 用vector容器代替数组 ——使用数组初始化vector对象
在C++中,我们不能用数组直接初始化另一数组,而只能创建新的数组,然后显式的把原数组的元素逐个复制给新的数组. 按照C语言中的做法: const size_t arry_size=6; int int ...
- C#修改文件夹权限
using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Dire ...
- JS 之作用域链和闭包
1.JS无块级作用域 <script> function Main(){ if (1==1){ var name = "alex"; } console.log(nam ...