hdu 2112 HDU Today(map hash)
来源:互联网 发布:怎么上淘宝精品推荐 编辑:程序博客网 时间:2024/04/29 23:37
/*
今天终于把这个题目过了,原来是起始点忘记了hash
虽然浪费了很多的时间,但是还是有收获的,至少map hash会了一点
做题目真的要思考全面,不能有一点错误的,特别是ACM,伟大的教训
*/
#include <iostream>//2563669 2010-06-30 19:18:23 Accepted 2112 2062MS 4296K 1680 B C++ 悔惜晟
#include <map>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
const int N = 1008;
const int MAX = 0x3fffffff;
int dis[N];
int cost[N][N];
bool hash[N];
map<string, int> df;
void Dijsktra(int len, string s, string end)
{
int i, j, k;
for(i = 1; i < len; i++)
{
dis[i] = cost[df[s]][i];
}
//printf("%d==/n",len);
memset(hash, false, sizeof(hash));
hash[df[s]] = true;
dis[df[s]] = 0;
for(i = 2; i < len; i++)
{
int d = MAX;
k = df[s];
for(j = 1; j < len; j++)
if(!hash[j] && d > dis[j])
{
k = j;
d = dis[j];
}
hash[k] = true;
for(j = 1; j < len; j++)
if(!hash[j] && dis[j] > dis[k] + cost[k][j])
dis[j] = dis[k] + cost[k][j];
}
if(dis[df[end]] != MAX)
cout<<dis[df[end]]<<endl;
else
cout<<"-1"<<endl;
}
int main()
{
int i, j, len, n, t;
//string ss, en;
//string start, end;
char ss[55], en[55];
char start[55], end[55];
while(scanf("%d", &n) != EOF && n != -1)
{
len = 1;
//cin>>start>>end;
scanf("%s %s", start, end);//起始点可能不在公交线路中,所以这个也要hash
if(!(df[start]))
{
df[start] = len;
len++;
//printf("%d===/n", len);
}
if(!(df[end] ))
{
df[end] = len;
len++;
//printf("%d===/n", len);
}
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
{
cost[i][j] = MAX;
}
for(i = 0; i < n; i++)
{
// cin>>ss>>en>>t;
scanf("%s %s %d", ss, en ,&t);
if(!(df[ss]))
{
df[ss] = len;
len++;
//printf("%d===/n", len);
}
if(!(df[en] ))
{
df[en] = len;
len++;
//printf("%d===/n", len);
}
//cout<<len<<endl;
cost[df[ss]][df[en]] = t;
cost[df[en]][df[ss]] = t;
}
//cout<<len<<endl;
Dijsktra(len, start, end);
df.clear();
}
}
- hdu 2112 HDU Today(map hash)
- 2112 HDU Today【dijkstra+map】
- HDU——2112HDU Today(SPFA+简单Hash或map+前向星)
- HDU-#2112 HDU Today(Dijkstra+map)
- hdu 2112 HDU Today (Dijkstra + map)
- HDU 2112 HDU Today (map+dijkstra)
- HDU 2112 HDU Today(floyd-warshall+map)
- hdu-2112-HDU Today(dijkstra + map)
- hdu 2112 HDU Today(map+Floyd)
- hdu 2112HDU Today 最短路+map
- hdu 2112 HDU Today (map+DIJ)
- hdu 2112 HDU Today(Dijtktra+map)
- hdu 2112 HDU Today(SPFA+map)
- HDU 2112 HDU Today【dijkstra+map】
- HDOJ-2112HDU Today(Floyd+MAP)
- HDU 2112 HDU Today
- hdu 2112 HDU Today
- Hdu-2112 HDU Today
- 检验CC++free和delete
- Local System/Network Service/Local Service权限详解
- BLAS+BLACS+LAPACK+SCALAPACK安装
- 数组元素交叉排列的算法题(a1 a2 a3 .. an b1 b2 b3 .. bn -->a 1 b1, a2 b2, a3 b3, .. an bn )
- 内核对象
- hdu 2112 HDU Today(map hash)
- 数组元素交叉排列的算法题(a1 a2 a3 .. an b1 b2 b3 .. bn -->a 1 b1, a2 b2, a3 b3, .. an bn ) 概论思想(perfect shuffle 算法)
- 正则表达式实战
- 学习
- 正则表达式基础知识
- 经常出现正常运行Windows所需的文件已被替换成无法识别的版本
- 数据结构--栈
- apache安装
- PHP——实现Zend Debug