HUD-2112 HDU Today(最短路map标记)
题目链接:HUD-2112 HDU Today

思路:
1.最短路spfa模板.
2.map标记建图.
3.考虑距离为0或者-1的情况.
总结:下次map记得清空orz。
AC代码:
#include<cstdio>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int Map[maxn][maxn];
int n;
int d[maxn];
int vis[maxn];
void spfa(int s,int n)
{
queue<int> q;
for(int i = ;i <= n;i++)
{
d[i] = INF;
vis[i] = ;
}
d[s] = ;
vis[s] = ;
q.push(s);
while(!q.empty())
{
int k = q.front();q.pop();
vis[k] = ;
for(int j = ;j <= n;j++)
{
if(d[k] + Map[k][j] < d[j])
{
d[j] = d[k] + Map[k][j];
if(!vis[j])
q.push(j),vis[j] = ;
}
}
}
}
void init()
{
for(int i = ;i <= ;i++)
{
for(int j = ;j <= i;j++)
{
if(j != i)
Map[i][j] = Map[j][i] = INF;
else Map[i][j] = ;
}
}
}
int main()
{
char a[],b[];
map<string,int> mp;
char st[],ed[];
int w;
while(~scanf("%d",&n) && n!= -)
{
init();//初始化
mp.clear();
int count = ;//用于map一对一转化;
scanf("%s%s",&a,&b);
mp[a] = ++count;
if(!mp[b]) mp[b] = ++count;//如果终点未出现过,则加入图中;
for(int i = ;i < n;i++)
{
scanf("%s %s %d",&st,&ed,&w);
if(!mp[st]) mp[st] = ++count;//检查两点是否出现过
if(!mp[ed]) mp[ed] = ++count;
int u = mp[st],v = mp[ed];
Map[u][v] = Map[v][u] = w;//建图;
}
spfa(,count);
if(d[ mp[b] ] == INF) printf("-1\n");
else printf("%d\n",d[ mp[b] ]);
}
return ;
}
HUD-2112 HDU Today(最短路map标记)的更多相关文章
- HDU 2112 HDU Today(STL MAP + Djistra)
题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <i ...
- HDU 2112 HDU Today(最短路径+map)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2112 HDU Today 最短路
题目描述: Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这 ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- hdu 2112 HDU Today
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的 ...
- HDU 2112 HDU Today(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
随机推荐
- 13 个设计 REST API 的最佳实践
原文 RESTful API Design: 13 Best Practices to Make Your Users Happy 写在前面 之所以翻译这篇文章,是因为自从成为一名前端码农之后,调接口 ...
- vue 弹框产生的滚动穿透问题
首先定义一个全局样式: .noscroll{ position: fixed; left: 0; top: 0; width: 100%; } 创建一个dom.js文件,定义几个方法: export ...
- 什么是http协议??
一.http协议的定义: http(Hypertext transfer protocol)超文本传输协议,通过浏览器和服务器进行数据交互,进行超文本(文本.图片.视频等)传输的规定.也就是说,htt ...
- SCUT - 31 - 清一色 - dfs
https://scut.online/p/31 还是不知道为什么RE了.的确非常玄学. 重构之后就没问题了.果然写的越复杂,分的情况越乱就越容易找不到bug. #include<bits/st ...
- SQL语句分类
SQL Structured Query Language SQL是结构化查询语言,是一种用来操作RDBMS的数据库语言,当前关系型数据库都支持使用SQL语言进行操作,也就是说可以通过 SQL 操作 ...
- node npm vue.js 笔记
cnpm 下载包的速度更快一些. 地址:http://npm.taobao.org/ 安装cnpm: npm install -g cnpm --registry=https://registry.n ...
- Linux手册页内容
总用9页 man1:可执行程序或shell命令 man2:系统调用(kernel提供的函数) man3:库调用(程序库中的函数) man4:/dev中的特殊文件 ...
- cat 合并文件或查看文件内容
1.命令功能 cat 合并文件或者查看文件内容. 2.语法格式 cat option file 参数说明 参数 参数说明 -n 打印文本,并显示每行行号并且空白行也同样包括 -b 与-n用法 ...
- Ubuntu环境下SSH服务安装、SSH远程登录以及SSH数据传输
https://www.cnblogs.com/asyang1/p/9467646.html SSH 为 Secure Shell 的缩写,为建立在应用层基础上的安全通信协议. 一.检查SSH服务是否 ...
- JS基础入门篇(七)—运算符
1.算术运算符 1.算术运算符 算术运算符:+ ,- ,* ,/ ,%(取余) ,++ ,-- . 重点:++和--前置和后置的区别. 1.1 前置 ++ 和 后置 ++ 前置++:先自增值,再使用值 ...