无向图 用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 ;
}

最新文章

  1. C#/ASP.NET完善的DBHelper,配套Model生成器
  2. Nginx配置加入css缓存配置后,css等文件not found
  3. MUI 版本更新
  4. TdxAlertWindowManager右下角HINT显示控件
  5. dotnet与各种数据库类型对应关系
  6. COJ 0579 4020求次短路的长度
  7. python学习第六天 条件判断和循环
  8. 怎样使用 iOS 7 的 AVSpeechSynthesizer 制作有声书(1)
  9. 第三篇:一个Spark推荐系统引擎的实现
  10. 学习pthreads,多线程的创建和终止
  11. python使用rabbitMQ介绍四(路由模式)
  12. 动态规划dp
  13. .net core2 笔记
  14. 【Thymeleaf】Thymeleaf模板对没有结束符的HTML5标签解析出错的解决办法
  15. Shell-cat url-list.txt | xargs wget -c
  16. PHP操作MongoDB 数据库
  17. 【译】理解JavaScript中的柯里化
  18. Linux命令学习总结:date命令【转】
  19. Flutter学习笔记与整合
  20. SVN cleanup 报错,清除svn的工作队列

热门文章

  1. 通用Excel文件导出工具类
  2. C#自绘蒙版控件,带延时隐藏显示,拷贝底图功能
  3. IDEA不生成WAR包,报错
  4. u-boot移植(八)---代码修改---存储控制器--MMU
  5. iOS 中的 xml 解析
  6. Mask RCNN 学习笔记
  7. weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP
  8. ditto复制增强
  9. ubuntu 禁用自带的nouveau显卡驱动,安装NVIDIA显卡驱动
  10. MIPI协议学习总结(一)