HDU 2112 HDU Today(STL MAP + Djistra)
题目链接:HDU Today
立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map
题目非常水,可是错了N遍。手贱了。本题不优点理的就是把地名转化为数字
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#define N 155
#define INF 1e7
using namespace std; int m[N][N],dis[N];
int vis[N];
int edge,n;
int Djistra()
{
for(int i = 0;i<=edge;i++)
{
vis[i] = 0;
dis[i] = m[1][i];
}
vis[1] = 1;
dis[1] = 0;
int u,mi;
for(int i = 1;i<=edge;i++)
{
u = 1; mi = INF;
for(int j = 1;j<=edge;j++)
{
if(!vis[j] && dis[j]<mi)
{
u = j;
mi = dis[j];
}
}
vis[u] = 1;
for(int j = 1;j<=edge;j++)
{
if(!vis[j] && m[u][j] < INF && dis[u] + m[u][j] < dis[j])
{
dis[j] = dis[u] + m[u][j];
}
}
}
return dis[2];
} /*int Floyd()
{
for(int k=1;k<edge;k++)
for(int i=1;i<edge;i++)
{
for(int j=1;j<edge;j++)
if(m[i][j]>m[i][k]+m[k][j])
m[i][j]=m[i][k]+m[k][j];
}
return m[1][2];
}*/
int main()
{
char s[32],e[32];
int w;
map<string,int>ma;
while(scanf("%d",&n)!=EOF)
{
if(n==-1) break;
int st = 0;
ma.clear();
scanf("%s%s",s,e);
if(strcmp(s,e)==0) //这个地方别家continue,后台还没输数据呢
st = 1; for(int i = 0;i<N;i++)
{
for(int j = 0;j<N;j++)
{
if(i==j)
m[i][j] = 0;
else
m[i][j] = INF;
}
}
ma[s] = 1; ma[e] = 2;
edge = 3;
for(int i = 0;i<n;i++)
{
scanf("%s%s%d",s,e,&w);
if(ma[s]==0) ma[s] = edge++;
if(ma[e]==0) ma[e] = edge++;
if(w < m[ma[s]][ma[e]])
{ m[ma[s]][ma[e]] = w;
m[ma[e]][ma[s]] = w;
}
}
if(st==1)
{
printf("0\n");
continue;
}
int sum = Djistra();
if(sum<INF)
printf("%d\n",sum);
else
printf("-1\n");
}
return 0;
}
一位ACMer,是这样处理 字符转化数字。挺不错的
char str1[10005][40],str2[10005][40],str[10005][40];
int judge(char *p)
{
for(int i = 1;i<=f;++i)
if(!strcmp(str[i],p))
return i;
strcpy(str[++f],p);
return f;
}
for(int i = 1;i<=n;++i)
scanf("%s%s%d",str1[i],str2[i],&str3[i]);
for(int i = 1;i<=n;++i)
{
int x = judge(str1[i]);
int y = judge(str2[i]);
map[x][y] = map[y][x] = str3[i];
}
HDU 2112 HDU Today(STL MAP + Djistra)的更多相关文章
- HDU——2723Electronic Document Security(STL map嵌套set做法)
Electronic Document Security Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2094 产生冠军(STL map)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 4393 Throw nails(STL之优先队列)
Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...
- 2018 ICPC 徐州网络预赛 Features Track (STL map pair)
[传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 3400 Line belt (三分再三分)
HDU 3400 Line belt (三分再三分) ACM 题目地址: pid=3400" target="_blank" style="color:rgb ...
随机推荐
- java修炼
java程序员修炼之道——大牛告诉我们应该好好学习与修炼以下知识与技能 ———————————————————— 一:Java语言学习(对线程(thread),串行化,反射,网络编程,JNI技术, ...
- 洛谷 P1027 Car的旅行路线
P1027 Car的旅行路线 题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路 ...
- 【BZOJ1874】取石子游戏(SG函数)
题意:小H和小Z正在玩一个取石子游戏. 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子, 每次取石子的个数有限制,谁不能取石子时就会输掉游戏. 小H先进行操作, 他想问你他是否有必 ...
- Extension Methods "点"函数方法 扩展方法
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...
- 怎样让enum枚举支持string
原文发布时间为:2011-03-02 -- 来源于本人的百度文章 [由搬家工具导入] 大家都知道enum是以下两种情况,不能支持string 1,enum类型是静态 2,enum类型仅限于long、i ...
- js7:表单的学习,Forms对象
原文发布时间为:2008-11-09 -- 来源于本人的百度文章 [由搬家工具导入] dreamveawer中,选择插入——表单——然后后面的几个选项进行学习: 大体上这么些类型: <html& ...
- css3 实现多行文本折行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- shell特殊符号用法大全
# 注释符号(Hashmark[Comments]) 1.在shell文件的行首,作为shebang标记,#!/bin/bash; 2. 其他地方作为注释使用,在一行中,#后面的内容并不会被执行, ...
- HBase shell 中的十六进制数值表示
在使用Hbase shell 进行get 或scan操作时,时不时会看到一些数值被转成了16进制, 就像下面那样 value=W\x5C5\x80 那么这个值具体等于多少? 查阅资料后发现算法如下 W ...
- hdu 2824(欧拉函数)
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...