hdu 2112 最短路
本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点
就是个最短路
Sample Input
6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1 Sample Output
50 Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。 ――全剧终――
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
const int maxint=;
//printf("----\n");
using namespace std;
int m,t;
int tot=;
map<string,int> st;
int c[][],dist[];
void dijkstra(int u,int n)
{
//printf("%d\n",n);
bool vis[];
for(int i=;i<=n;i++)
{
vis[i]=;
dist[i]=c[u][i];
}
vis[u]=;
for(int i=;i<=n;i++)
{
int temp=maxint;
int k=u;
for(int j=;j<=n;j++)
{
if(!vis[j]&&dist[j]<temp)
{
temp=dist[j];
k=j;
}
}
if(temp==maxint) break;
vis[k]=;
for(int j=;j<=n;j++)
{
if(!vis[j]&&c[k][j]<maxint)
{
if(dist[k]+c[k][j]<dist[j])
dist[j]=dist[k]+c[k][j];
}
}
}
}
int main()
{
int i,j,k,n;
//freopen("1.in","r",stdin);
while(scanf("%d",&n)!=EOF)
{
bool flag=;
if(n==-) break;
st.clear();
char s[],e[];
scanf("%s%s",s,e);
if(strcmp(s,e)==)
{
flag=;
}
st[s]=;
st[e]=;
tot=;
for(i=;i<=;i++)
{
for(j=;j<=;j++) c[i][j]=c[j][i]=((i==j)?:maxint);
}
for(i=;i<=;i++)
{
dist[i]=maxint;
}
for(i=;i<=n;i++)
{
int w;
scanf("%s%s%d",s,e,&w);
if(!st[s]) st[s]=tot++;
if(!st[e]) st[e]=tot++;
c[st[s]][st[e]]=c[st[e]][st[s]]=w;
}
dijkstra(,tot);
if(flag) printf("0\n");
else if(dist[]==maxint) printf("-1\n");
else
printf("%d\n",dist[]);
}
return ;
}
2015-04-24:这个是kuangbin模板的,检测了一下,算法里的起点是从0开始的,那个优化代码不知道怎么就wa了,等刷到kuang大神的最短路再看吧
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
#define typec int
const typec INF=;//防止后面溢出,这个不能太大
int c[MAXN][MAXN],tot=,dist[];
bool vis[MAXN];
int pre[MAXN];
void Dijkstra(typec cost[][MAXN],typec lowcost[],int n,int beg)
{
for(int i=;i<=n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<=n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)break;
vis[k]=true;
for(int i=;i<=n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
}
}
map<string,int> st;
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
bool flag=;
if(n==-) break;
st.clear();
char s[],e[];
scanf("%s%s",s,e);
if(strcmp(s,e)==)
{
flag=;
}
st[s]=;
st[e]=;
tot=;
for(i=;i<=;i++)
{
for(j=;j<=;j++) c[i][j]=c[j][i]=((i==j)?:INF);
}
for(i=;i<=;i++)
{
dist[i]=INF;
}
for(i=;i<=n;i++)
{
int w;
scanf("%s%s%d",s,e,&w);
if(!st[s]) st[s]=tot++;
if(!st[e]) st[e]=tot++;
c[st[s]][st[e]]=c[st[e]][st[s]]=w;
}
Dijkstra(c,dist,tot,);
if(flag) printf("0\n");
else if(dist[]==INF) printf("-1\n");
else
printf("%d\n",dist[]);
}
}
hdu 2112 最短路的更多相关文章
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...
- 【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)
http://acm.hdu.edu.cn/showproblem.php?pid=2112 这道题给了一个将字符串与int对应的思路,就是使用map 这道题答案对了,但是没有AC,我也不知道为什么. ...
- hdu 2112 HDU Today (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...
- HDU 2112 HDU Today 最短路
题目描述: Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这 ...
随机推荐
- zTree静态树与动态树的用法——(七)
0.[简介] zTree 是利用 JQuery 的核心代码,实现一套能完成大部分常用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同时生成多个 Tree 实 ...
- RabbitMQ学习(一):RabbitMQ要点简介
转载:http://blog.csdn.net/leixiaotao_java/article/details/78909760#t0 1.什么是RabbitMQ? RabbitMQ是由Erlang语 ...
- Jenkins无法安装插件或首次安装插件界面提示Offline
一.首先点击系统管理 二.点击插件管理 三.选择高级管理 四.将升级站点中的https改成http即可
- haproxy支持的负载均衡算法详解
目前haproxy支持的负载均衡算法有如下8种: 1.roundrobin 表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法.该算法是动态的,对于实 ...
- MySQL 导入CSV数据
第一步 创建表结构 create table t1( key1 ), v1 ) ); 第二步 导入数据 load data local infile 'D:/t1.csv' into table t1 ...
- 二、springboot配置
一.启动类 在包根目录下添加启动类,必须包含main方法,再添加Spring Boot启动方法: SpringApplication.run(SampleController.class, args) ...
- 21 JSON and Go go语言和json
JSON and Go 25 January 2011 Introduction JSON (JavaScript Object Notation) is a simple data intercha ...
- Python爬虫之三种网页抓取方法性能比较
下面我们将介绍三种抓取网页数据的方法,首先是正则表达式,然后是流行的 BeautifulSoup 模块,最后是强大的 lxml 模块. 1. 正则表达式 如果你对正则表达式还不熟悉,或是需要一些提 ...
- Jmeter运行结果unicode编码乱码问题
一.web页面乱码 比如访问百度返回页面显示乱码,如下会有问号 如果想让他显示中文可以按以下操作: 1.打开jmter配置文件 bin/jmeter.properties 2.修改配置文件,查找“sa ...
- hihoCoder #1184 : 连通性二·边的双连通分量(边的双连通分量模板)
#1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...