hd 2112 HDU Today
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
#include <stdio.h>
#include <string.h>
#include <queue>
#define inf 0x3f3f3f3f
#define N 1000000
using namespace std;
int vis[], dis[], head[];
int n, cnt, k;
char s1[], s2[];
char sta[][];
struct note
{
int from, to, cost, next;
}edge[N];
void add(int x, int y, int z)
{
note e = {x, y, z, head[x]};
edge[cnt] = e;
head[x] = cnt++;
}
void spfa()
{
queue<int>q;
memset(vis, , sizeof(vis));
memset(dis, inf, sizeof(dis));
q.push();
vis[] = ;
dis[] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].cost)
{
dis[v] = dis[u] + edge[i].cost;
if(!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
if(dis[] == inf)
printf("-1\n");
else
printf("%d\n", dis[]);
}
int main()
{
int a, b, c;
while(~scanf("%d", &n), n != -)
{
int flag;
k = , cnt = ;
scanf("%s%s", sta[], sta[]);
memset(head, -, sizeof(head));
for(int i = ; i < n; i++)
{
scanf("%s%s%d", s1, s2, &c);
flag = ;
for(int i = ; i <= k; i++)
{
if(strcmp(s1, sta[i]) == )
{
flag = ;
a = i;
break;
}
}
if(flag)
{
strcpy(sta[++k], s1);
a = k;
}
flag = ;
for(int i = ; i <= k; i++)
{
if(strcmp(sta[i], s2) == )
{
b = i;
flag = ;
break;
}
}
if(flag)
{
strcpy(sta[++k], s2);
b = k;
}
add(a, b, c);
add(b, a, c);
}
if(strcmp(sta[], sta[]) == )
{
printf("0\n");
continue;
}
spfa();
}
return ;
}
floyd算法:
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
#define N 160
int dis[N][N];
int n;
char s1[], s2[], str[][];
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;
}
}
int min(int x, int y)
{
return x < y ? x : y;
}
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++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
int main()
{
while(~scanf("%d", &n), n != -)
{
int k = , a, b, t, flag;
init();
scanf("%s%s", s1, s2);
strcpy(str[], s1);
strcpy(str[], s2);
while(n--)
{
scanf("%s%s%d", s1, s2, &t);
flag = ;
for(int i = ; i <= k; i++)
{
if(strcmp(str[i], s1) == )
{
flag = ;
a = i;
break;
}
}
if(flag)
{
strcpy(str[++k], s1);
a = k;
}
flag = ;
for(int i = ; i <= k; i++)
{
if(strcmp(str[i], s2) == )
{
flag = ;
b = i;
break;
}
}
if(flag)
{
strcpy(str[++k], s2);
b = k;
}
dis[a][b] = dis[b][a] = t;
}
floyd();
if(strcmp(str[], str[]) == )
{
printf("0\n");
continue;
}
if(dis[][] != INF)
printf("%d\n", dis[][]);
else
printf("-1\n");
}
return ;
}
hd 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 ...
- hdoj 2112 HDU Today
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=2112 分析:多了一个地方的条件,用map来映射地点编号,Dijkstra求解即可 //2013-10- ...
- hdu 2112 HDU Today (floyd算法)
这道题貌似在原来学长给我们的搞的小比赛中出过! 这次又让我遇到,果断拿下! 不过方法很蠢,跑了1000多ms,虽然要求5000ms以内! 题目就是给你一些位置之间的距离,然后再让你求特定的两点之间的距 ...
- hdu 2112 HDU Today (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...
- 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(最短路径+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强.这 ...
随机推荐
- Python 2.7_pandas连接MySQL数据处理_20161229
在我本地Mysql_local_db数据库建立了一个pandas数据表用来对pandas模块的学习 学习过程借鉴学习蓝鲸的网站分析笔记 1.创建表 CREATE TABLE pandastest( 城 ...
- Scala
简直就是java啊,mac上,下载,添加环境变量,source 就好了
- PHP实现异步调用方法研究
作者: Laruence 本文地址: http://www.laruence.com/2008/04/14/318.html 转载请注明出处 , ,); curl_setopt_array(, ); ...
- iOS开发中多线程间关于锁的使用
为什么需要使用锁,当然熟悉多线程的你,自然不会感到陌生. 那你在代码中是否很好的使用了锁的机制呢?你又知道几种实现锁的方法呢? main.m 1 int main(int argc, const ch ...
- LDAP与migrationtools 导入系统账号
1:安装migrationtools yum -y install migrationtools 2:修改配置文件 cd /usr/share/migrationtools 可以看到很多的文件 ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- modelsim搭建uvm环境及实例
Modelsim SE-64 10.2c 自带编译好的uvm-1.1d 脚本new_run.do set UVM_DPI_HOME C:/modeltech64_10.2c/uvm-1.1d/win6 ...
- C语言字符串拷贝
C语言字符串拷贝利用指针操作,要清楚知道指针的指向 代码如下: #include <stdio.h> #include <assert.h> #include <stri ...
- otter双主同步安装与配置
otter是阿里的开源数据同步项目,资源地址就不用说了哈,网上找,阿里云论坛关于单方向同步的配置已经很清楚了,理论上说,双主同步也不复杂,但是毕竟 是数据库,比较重要,配置双主的时候,总觉得心里没底, ...
- [转]Oracle 经验集
-- 转换字符串为日期格式 to_date('2011-12-30 11:54:30','yyyy-MM-dd:hh24:mi:ss') Oracel 用 C# 连接,Microsoft 自带的 Sy ...