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强.这 ...
随机推荐
- Makefile <网络转载>
陈皓 (CSDN)概述——什 么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的 ...
- LINUX退出当前进程——比较return、exit()
1.在Linux中任何让一个进程退出 进程退出表示进程即将结束.在Linux中进程退出分为了正常退出和异常退出两种. 1>正常退出 a. 在main()函数中执行return . b.调用exi ...
- Excel 读写程序 C#源代码下载
http://u.163.com/lNaJAjOz 提取码: E4ZHjnfD
- 循序渐进Python3(十)-- 0 -- RabbitMQ
RabbitMQ RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息 ...
- 史航第12次作业&总结
作业1:找出最长的字符串 #include <stdio.h> #include <string.h> int main() { ],strings[][]; ; printf ...
- @ViewDebug.ExportedProperty的使用
原文链接:http://daemon369.github.io/android/2014/06/12/android-viewdebug-exportedproperty/ http://www.eo ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- Chain Of Responsibility(职责连)-对象行为型模式
1.意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 2.动机 给多个对象处理一个请求的机会,从而 ...
- iScroll-5 API 中文版
http://wiki.jikexueyuan.com/project/iscroll-5/ http://www.mamicode.com/info-detail-331827.html IScro ...
- dockManager中DockPanel的刷新问题!
使用的是DevExpress的dockManager控件,新增一个DockPanel,在DockPanel中添加一个AxMapControl控件并设置默认加载地图. 实现效果图如下: 但是存在一个问题 ...