HDU 2112 HDU Today 最短路
题目描述:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<string>
using namespace std;
const int MAX = 0xffffff,SIZE = ;
int Map[SIZE][SIZE];
char str1[][],str2[][];
int str3[];
int n,f;
map<string,int> map1;
int judge(char *p) {
string s1 = p;
pair<map<string,int>::iterator,bool> iter;
iter = map1.insert(pair<string,int> (s1,++f));
if(iter.second)
return f;
else {
f--;
return map1[s1];
}
}
int main() {
char sta[],end[];
int s,e;
while(scanf("%d",&n)&&n!=-) {
map1.clear();
scanf("%s%s",sta,end);
f = ;
for(int i = ;i<=n;++i)
scanf("%s%s%d",str1[i],str2[i],&str3[i]);
if(!strcmp(sta,end)) {
printf("0\n");
continue;
}
for(int i = ;i<SIZE;++i) {
Map[i][i] = ;
for(int j = ;j<SIZE;++j)
Map[i][j] = MAX;
} for(int i = ;i<=n;++i) {
int x = judge(str1[i]);
int y = judge(str2[i]);
Map[x][y] = Map[y][x] = str3[i];
}
s = judge(sta);
e = judge(end);
for(int i = ;i<=f;++i)
for(int j = ;j<=f;++j)
for(int k = ;k<=f;++k)
Map[j][k] = std::min(Map[j][i]+Map[i][k],Map[j][k]);
if(Map[s][e]==MAX)
printf("-1\n");
else printf("%d\n",Map[s][e]);
}
return ;
}
没用STL的代码:
#include<cstdio>
#include<cstring>
#include<iostream>
const int MAX = 0xffffff,SIZE = ;
int map[SIZE][SIZE];
char str1[][],str2[][],str[][];
int str3[];
int n,f;
int judge(char *p) {
for(int i = ;i<=f;++i)
if(!strcmp(str[i],p))
return i;
strcpy(str[++f],p);
return f;
}
int main() {
char s1[],s2[],sta[],end[];
int s,e;
while(scanf("%d",&n)&&n!=-) {
scanf("%s%s",sta,end);
f = ;
for(int i = ;i<=n;++i)
scanf("%s%s%d",str1[i],str2[i],&str3[i]);
if(!strcmp(sta,end)) {
printf("0\n");
continue;
}
for(int i = ;i<SIZE;++i) {
map[i][i] = ;
for(int j = ;j<SIZE;++j)
map[i][j] = MAX;
}
for(int i = ;i<=n;++i) {
int x = judge(str1[i]);
int y = judge(str2[i]);
map[x][y] = map[y][x] = str3[i];
}
s = judge(sta);
e = judge(end);
for(int i = ;i<=f;++i)
for(int j = ;j<=f;++j)
for(int k = ;k<=f;++k)
map[j][k] = std::min(map[j][i]+map[i][k],map[j][k]);
if(map[s][e]==MAX)
printf("-1\n");
else printf("%d\n",map[s][e]);
}
return ;
}
HDU 2112 HDU Today 最短路的更多相关文章
- 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 2112 HDU Today (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2112 HDU Today (最短路,字符处理)
题目 题目很简单,只是多了对地名转化为数字的处理,好吧,这我也是参考网上的处理办法,不过大多数的人采用map来处理 注意初始化注意范围,不然会wa!!!(这是我当时wa的原因org) 大家容易忽视的地 ...
- hdu 2112 HDU Today 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloo ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 2112 HDU Today (floyd算法)
这道题貌似在原来学长给我们的搞的小比赛中出过! 这次又让我遇到,果断拿下! 不过方法很蠢,跑了1000多ms,虽然要求5000ms以内! 题目就是给你一些位置之间的距离,然后再让你求特定的两点之间的距 ...
- HDU 2112 HDU Today(STL MAP + Djistra)
题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <i ...
随机推荐
- android listview addheaderview viewpager
just set viewPager's onTouchListener,like this: viewPager.setOnTouchListener(new OnTouchListener() { ...
- hive orc压缩数据异常java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.hive.ql.io.orc.OrcSerde$OrcSerdeRow
hive表在创建时候指定存储格式 STORED AS ORC tblproperties ('orc.compress'='SNAPPY'); 当insert数据到表时抛出异常 Caused by: ...
- PAT甲题题解-1033. To Fill or Not to Fill (25)-模拟
模拟先说一下例子,最后为方便起见,在目的地安增加一个费用为0的加油站0 1 2 3 4 5 6 7 87.1 7.0 7.2 6.85 7.5 7.0 7.3 6.0 00 150 200 300 4 ...
- Alpha Version Release Of Teamwork: Appendix 1 BUG BASH
在为期一周的发布周中,我们将app本身最后的细节完善,功能代码到位,UI不断改进和优化,团队在开始准备发布之前,对整个APP进行了一次BUG检查,每个人都部署了app在自己的android设备上进行测 ...
- Alpha冲刺——事后诸葛亮
组长博客 作业博客 项目Postmortem 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件针对的是福大学子来到食堂会犹豫不决无法决定吃什么 ...
- Visual Studio 2015的安装和简单的单元测试
何为单元测试 绝大多数的软件都是由多人合作完成的,大家的工作相互有依赖关系.软件的很多错误都来源于程序员对模块功能的误解.疏忽或不了解其他模块的变化.如何能让自己负责的模块功能的定义尽量的明确,模块内 ...
- ElasticSearch 2 (33) - 信息聚合系列之聚合过滤
ElasticSearch 2 (33) - 信息聚合系列之聚合过滤 摘要 聚合范围限定还有一个自然的扩展就是过滤.因为聚合是在查询结果范围内操作的,任何可以适用于查询的过滤器也可以应用在聚合上. 版 ...
- [转帖][Bash Shell] Shell学习笔记
[Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html 阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...
- [转帖] k8s dashboard 的创建 升级 以及 admin token的创建和简单使用.
Kubernetes Dashboard中的身份认证详解 https://jimmysong.io/posts/kubernetes-dashboard-upgrade/ Thu Nov 2, 201 ...
- 用好SVN与Git,版本管理都不是问题
介绍一下SVN SVN:代码控制器(版本控制器),主要是为了多人协同开发项目,管理代码.也可以管理个人代码.也叫程序界的”后悔药“. SVN(是subversion的简称)是近年来一款基于C/S架构的 ...