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强.这 ...
随机推荐
- PL/SQ连接oracle,L 新建表的时候, virtual那一列是什么意思
Virtual标示该栏位是否为虚拟列. https://www.2cto.com/database/201306/216917.html
- python面向对象——类
from:http://www.runoob.com/python3/python3-class.html Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在P ...
- C++ 和 MFC的学习
1. 在Windows应用程序设计中,既可以使用个C的基本数据类型,也可以使用Windows自定义的数据类型.但要注意,凡是Windows自定义的关键字都要大写. 2. 什么是句柄? 在Windows ...
- github后端开发面试题大集合(二)
作者:小海胆链接:https://www.nowcoder.com/discuss/3615?type=0&order=0&pos=8&page=0来源:牛客网 7.非关系型数 ...
- ZooKeeper常见问题
转载自原文:zookeeper(二)常见问题汇总 一.为什么zookeeper要部署基数台服务器? 所谓的zookeeper容错是指,当宕掉几个zookeeper服务器之后,剩下的个数必须大于宕掉的个 ...
- Ubuntu 14.04 16.04 Linux nvidia 驱动下载与安装
Ubuntu 14.04 16.04 nvidia 驱动安装 最简单直观的方式是在如下的对话框中直接选择驱动安装即可 但是有时候,驱动不够新,比如14.04用的是340.98版本,如果手动安装驱动可以 ...
- Python之Selenium的爬虫用法
Selenium 2,又名 WebDriver,它的主要新功能是集成了 Selenium 1.0 以及 WebDriver(WebDriver 曾经是 Selenium 的竞争对手).也就是说 Sel ...
- 参数化SQL语句
避免SQL注入的方法有两种:一是所有的SQL语句都存放在存储过程中,这样不但可以避免SQL注入,还能提高一些性能,并且存储过程可以由专门的数据库管理员(DBA)编写和集中管理,不过这种做法有时候针对相 ...
- D - Round Subset codeforces837d
D - Round Subset 思路:背包: 代码: #include <cstdio> #include <cstring> #include <iostream&g ...
- jquery跨域请求事例
//js发送跨域请求部分var requesturl = 'url'; $.ajax({ type:'GET', url:requesturl, data:{'qNum':num}, dataType ...