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