hdu 2112 map+Dijkstra
无向图 用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
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <algorithm>
# include <cmath>
# include <queue>
# include <map>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=; int n ;
map<string,int> mp ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ; void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;
}
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]; }
} } int main ()
{
// freopen("in.txt","r",stdin) ;
int m ;
while(cin>>m)
{
if (m == -)
break ;
mp.clear() ;
string s1 ,s2 ;
int i , j , w ;
for (i = ; i < MAXN ; i++)
for (j = ; j < MAXN ; j++)
{
if (i == j)
cost[i][j] = ;
else
cost[i][j] = INF ;
} cin>>s1>>s2 ;
mp[s1] = ;
mp[s2] = ;
n = ;
bool flag = ;
if (s1 == s2)
flag = ;
while(m--)
{
cin>>s1>>s2>>w ;
if (!mp[s1])
mp[s1] = n++ ;
if (!mp[s2])
mp[s2] = n++ ;
if (w < cost[mp[s1]-][mp[s2]-])
{
cost[mp[s1]-][mp[s2]-] = w ;
cost[mp[s2]-][mp[s1]-] = w ;
} }
n-- ;
if (flag)
{
cout<<<<endl ;
continue ;
}
Dijkstra() ; if (lowcost[] != INF)
cout<<lowcost[]<<endl ;
else
cout<<-<<endl ; } return ;
}
hdu 2112 map+Dijkstra的更多相关文章
- HDU 2112 Today(Dijkstra+map)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意: 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050 ...
- 【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)
http://acm.hdu.edu.cn/showproblem.php?pid=2112 这道题给了一个将字符串与int对应的思路,就是使用map 这道题答案对了,但是没有AC,我也不知道为什么. ...
- hdu 2112 HDU Today(map与dijkstra的结合使用)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- HDU 2112 HDU Today(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others ...
- HDU Today HDU杭电2112【Dijkstra || SPFA】
http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...
- 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 Dijkstra
注意: 1.去重边 2.终点和起点一样,应当输出0 3.是无向图 AC代码 #include <cstdio> #include <cmath> #include <al ...
随机推荐
- Linux记录-shell一行代码杀死进程(收藏)
ps -ef |grep hello |awk '{print $2}'|xargs kill -9
- epoll讲解--转自知乎
Ctrip AIO底层使用epoll https://blog.csdn.net/z69183787/article/details/52943637 首先我们来定义流的概念,一个流可以是文件,soc ...
- 词典的实现(3)--使用JAVA类库ArrayList实现Map数据结构
1,在词典的实现(2)-借助顺序表(数组)实现词典文章中使用了自定义的数组代替ArrayList,并实现了Map数据结构的基本功能.而借助JAVA类库ArrayList类的一些方法可以更加容易地实现M ...
- GET_WHEEL_DELTA_WPARAM宏在C#
1.高位字,署名: ((short)(wParam>>16)) 2. 为了获得最大的清晰,我会定义一组这样的函数: internal static class NativeMethods ...
- ==========2014-04-24=========winform树控件勾选方法 和获取所有选中的
http://bbs.bccn.net/thread-197567-1-1.html /// <summary> /// 已选中或取消选中树节点上的复选框时 /// </summar ...
- 第15月第6天 ios UIScrollView不能响应TouchesBegin
1. 1:@property MyScrollView *scrollView; 2:给MyScrollView,增加类别:MyScrollView+Touch 3:在类别里实现下面三个方法: @im ...
- html 替换元素
参考博客:http://www.cnblogs.com/wkylin/archive/2011/05/12/2044328.html 可替换元素也是行内元素 替换元素是浏览器根据其标签的元素与属性来判 ...
- Adroid反编译资料收集
Android反编译神器jadx的使用 https://blog.csdn.net/Fisher_3/article/details/78654450 Android 反编译利器,jadx 的高级技巧 ...
- Netty通信原理
Netty是一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端,它极大的简化了TCP和UDP套接字服务器等网络编程. BIO(Blocking IO):每一个请求,一个S ...
- BN讲解(转载)
本文转载自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所讲的内容为Batch Normalization,简称BN,来源于< ...