hdu 2112:HDU Today
来源:互联网 发布:腾讯云如何解析端口号 编辑:程序博客网 时间:2024/04/30 01:46
给定一波站点,求其中两个站点的最短路径。
Map映射,floyd求最短路。注意map<string,int>ss这句话的存在使得要添加string的头文件。
#include <cstdio>#include <cstring>#include <string>#include <map>#include <iostream>using namespace std;const int inf = 1<<20 ;int way[155][155] ;char s[40] , e[40] ;char s1[40] , s2[40] ;int n , all ;map <string , int> ss ;void init() { ss.clear(); for(int i=1;i<=150;i++){ for(int j=i+1;j<=150;j++){ way[i][j]=way[j][i]=inf; } }}void floyd() { for(int k=1;k<=all;k++){ for(int i=1;i<=all;i++){ if(way[i][k]==inf) continue; for(int j=1;j<=all;j++){ if(i!=j && i!=k && j!=k && way[i][j]>way[i][k]+way[k][j]){ way[i][j]=way[i][k]+way[k][j]; } } } }}int main() { //freopen("in.txt","r",stdin); while(cin >> n) { if(n==-1) break; init(); scanf("%s %s",s,e); ss[s]=1 ; int i=2; while(n--){ int d; scanf("%s %s %d",s1,s2,&d); if(!ss[s1]) ss[s1]=i++; if(!ss[s2]) ss[s2]=i++; way[ss[s1]][ss[s2]]=way[ss[s2]][ss[s1]]=d; } if(!ss[e]) { cout << -1 << endl ; continue; } all=i-1; int nn=ss[e]; floyd(); if(way[1][nn]==inf) cout << -1 << endl; else cout << way[1][nn] << endl; } return 0;}
0 0
- HDU 2112 HDU Today
- hdu 2112 HDU Today
- Hdu-2112 HDU Today
- hdu 2112 HDU Today
- HDU 2112 HDU Today
- hdu 2112 HDU Today
- HDU 2112 HDU Today
- hdu 2112 HDU Today
- HDU 2112 HDU Today
- HDU 2112 HDU Today
- HDU 2112 HDU Today
- hdu 2112 hdu today
- hdu 2112 HDU Today
- hdu 2112 HDU Today
- hdu 2112 HDU Today
- hdu 2112 HDU Today
- HDU 2112 HDU Today
- hdu 2112:HDU Today
- 深入探讨PageRank(二):PageRank原理剖析
- 文件系统,磁盘性能测试
- 选择、冒泡、插入-排序
- printf&scanf的格式化代码详解
- 学习android 一个月开发的看小说app(文云阁)
- hdu 2112:HDU Today
- 用PHP校验EMAIL地址的正确方法
- 关于jquery的绑定方法
- cigarettes
- java并发编程实践学习笔记
- CF 394A:Counting Sticks
- JNDI 在 J2EE 中的角色
- java招聘技术准备
- 精 挑 细 选