hdu 2680 Choose the best route
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2680
简单最短路问题。。。。。
运行结果:
Accepted | 2680 | 265MS | 4164K | 1382 B | C++ |
//dijkstra
#include<stdio.h>
#define INF (1<<30)
#define MAXN 1005
int map[MAXN][MAXN],n,m,s,d[MAXN],dis[MAXN];
bool vis[MAXN];
void dijkstra(int start)
{int i,j,k;for(i = 1; i <= n; i++)//初始化所有点为未访问过,起点到所有其他点的距离为无穷远vis[i] = false,dis[i] = INF;//将起点到个顶点的距离保存在dis数组中for(i = 1; i <= n; i++)dis[i] = map[start][i];//起点到其本身的距离为0,并将其标记为已经访问过dis[start] = 0;vis[start] = true;//在将n-1个顶点加入到最短路中for(i = 1; i < n; i++){int min = INF;for(j = 1; j <= n; j++){if(!vis[j] && dis[j] < min){min = dis[j];k = j;}}if(min == INF)continue;vis[k] = true;for(j = 1; j <= n; j++){if( !vis[j] && dis[j] > min + map[k][j])dis[j] = min + map[k][j];}}
}
int main()
{int i,j,start,end,cost,w;while(scanf("%d%d%d",&n,&m,&s) != EOF){for(i = 0; i <= n; i++)for(j = 0; j <= n; j++){map[i][j] = (i == j ? 0 : INF);}for(i = 0; i < m; i++){scanf("%d%d%d",&end,&start,&cost);if(map[start][end] > cost)map[start][end] = cost;}dijkstra(s);scanf("%d",&w);int min = INF;while( w-- ){scanf("%d",&end);if(dis[end] < min)min = dis[end];}if(min == INF)printf("-1\n");elseprintf("%d\n",min);}return 0;
}
运行结果:
Accepted | 2680 | 125MS | 496K | 1270 B | C++ |
//spfa
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF (1<<30)
struct node
{int v,cost;node *next;
}*head[1005],edge[20005];
int dis[1005];
bool vis[1005];
int n,m,s;
queue<int> que;
void spfa(int start)
{int i = 0;for(i = 1; i <= n; i++)dis[i] = INF;memset(vis,false,sizeof(vis));vis[start] = true;dis[start] = 0;que.push(start);while( !que.empty() ){int now = que.front();que.pop();vis[now] = false;for(node *p = head[now]; p ; p = p->next){if(dis[p->v] > dis[now] + p->cost){dis[p->v] = dis[now] + p->cost;if( !vis[p->v]){vis[p->v] = true;que.push(p->v);}}}}
}
int main()
{int i,start,end,cost,w,min;node *p;while(scanf("%d%d%d",&n,&m,&s) != EOF){for(i = 1; i <= n; i++)head[i] = NULL;p = edge;for(i = 0; i < m; i++){scanf("%d%d%d",&start,&end,&cost);p->v = start;p->cost = cost;p->next = head[end];head[end] = p++;}spfa(s);scanf("%d",&w);min = INF;while( w-- ){scanf("%d",&start);if(dis[start] < min)min = dis[start];}if(min == INF)printf("-1\n");elseprintf("%d\n",min);}return 0;
}
转载于:https://www.cnblogs.com/LUO257316/archive/2012/09/03/3220864.html
hdu 2680 Choose the best route相关推荐
- HDU 2068 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
- hdu 1599 find the mincost route(找无向图最小环)(floyd求最小环)
ps(我到今天才知道Floyd的核心思想是动态规划==) hdu 1599 find the mincost route(找无向图最小环) 注意!这里写成 #define data 0x3f3f3 ...
- Choose the best route
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arr ...
- HDU 1599 find the mincost route
Floyd可解. 求最短.在路上来回.使用Floyd 而在 三同时不 找出最短.然后更新. 没有推理启动!=INF. 一堆负面结果溢出. #include<cstdio> #include ...
- AcWing 1137. Choose the best route(朴素dijkstra反向建图 or 虚拟源点法)
题目比较简单,讲两种做法 法一.二都是用的朴素dijkstra算法 法一:反向建图 求终点s到每个起点的最短距离 O(T * (n^2 + n))(T表示多组测试数据)820ms #include & ...
- 杭电OJ分类题目(4)-Graph
原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...
- 训练日记 2019.9.9 重阳节快乐
2019.9.9 今天也没啥太好说的,不过就是发现了几点最短路问题中常见到的问题,写一下吧. 最短路,看了半圈csdn感觉很迷,因为好像大部分人都没搞清dijkstra和spfa到底有什么区别,所有问 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【HDOJ图论题集】【转】
1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...
最新文章
- Python计算机视觉——图像到图像的映射
- HTML中GET与POST的区别
- 敲代码就是一把梭_2020必看!开发五年的大佬日常工作中所使用的java代码技巧...
- MATLAB实现牛顿插值的源程序
- SCRUM与XP区别
- VB6 无法加载ieframe.dll,导致不能使用 浏览器控件--(转载)
- java jframe 设置背景图片_JFrame如何设置背景图片
- Java二十三种设计模式 之代理(proxy)
- iOS利用Speech Kit实现语音识别
- java 面向对象 1
- 学python数据分析心得体会800字_一点学习大数据分析的心得体会
- 十七.降维之谱分解和奇异值分解
- 废旧手机变成远程摄像头
- CentOS 8: yum设置为阿里源
- 初入Kaggle之数据集的使用及预测结果生成
- 题朋友云南骑马照【明书、圣契】
- 从隐函数存在定理到隐函数定理
- 利用Shell 脚本导出sql表格并邮寄发送
- Mysql关于主键自增后的插入语句编写
- MapReduceJob提:交流程之JobClient端分析