1031. Campus
来源:互联网 发布:淘宝联盟提现钱到哪里 编辑:程序博客网 时间:2024/04/20 02:27
1031. Campus
Description
At present, Zhongshan University has 4 campuses with a total area of 6.17 square kilometers sitting respectively on both sides of the Pearl River or facing the South China Sea. The Guangzhou South Campus covers an area of 1.17 square kilometers, the North Campus covers an area of 0.39 square kilometers, the Guangzhou East Campus has an area of 1.13 square kilometers and the Zhuhai Campus covers an area of 3.48 square kilometers. All campuses have exuberance of green trees, abundance of lawns and beautiful sceneries, and are ideal for molding the temperaments, studying and doing research.
Sometime, the professors and students have to go from one place to another place in one campus or between campuses. They want to find the shortest path between their source place S and target place T. Can you help them?
Input
The first line of the input is a positive integer C. C is the number of test cases followed. In each test case, the first line is a positive integer N (0<N<=100) that represents the number of roads. After that, N lines follow. The i-th(1<=i<=N) line contains two strings Si, Ti and one integer Di (0<=Di<=100). It means that there is a road whose length is Di between Si and Ti. Finally, there are two strings S and T, you have to find the shortest path between S and T. S, T, Si(1<=i<=N) and Ti(1<=i<=N) are all given in the following format: str_Campus.str_Place. str_Campus represents the name of the campus, and str_Place represents the place in str_Campus. str_Campus is "North", "South", "East" or "Zhuhai". str_Place is a string which has less than one hundred lowercase characters from "a-z". You can assume that there is at most one road directly between any two places.
Output
The output of the program should consist of C lines, one line for each test case. For each test case, the output is a single line containing one integer. If there is a path between S and T, output the length of the shortest path between them. Otherwise just output "-1" (without quotation mark). No redundant spaces are needed.
Sample Input
12South.xiaolitang South.xiongdelong 2South.xiongdelong Zhuhai.liyuan 100South.xiongdelong South.xiaolitang
Sample Output
2
Problem Source
ZSUACM Team Member
#include <iostream>#include <map>#include <string>#include <cstring>using namespace std;const int INF=100000;//自定义无穷大int adj[205][205];//记录两点间距离bool flag[205];//标记处理过的点int dis[205];//记录各点到某点的距离int t,road,d,n;//t为测试数据组数,road为每组数据行数,d为没行数据表示的两点间距离,n记录映射对应的路的编号string start,send;//每行输入的两条路int dijkstra(int a,int b)//dijkstra法处理a到b的最短路{memset(flag,0,sizeof(flag));for(int i=0;i<n;i++)dis[i]=((i==a)?0:INF);//其他各点初始化距起点为INFfor(int i=0;i<n;i++){int min=INF,x=a;///x为要求出的未标记点中距离a最近的点for(int y=0;y<n;y++){if(!flag[y]&&dis[y]<min){min=dis[y];x=y;}}flag[x]=1;//最近的点标记,下次则从未标记的点中选取for(int y=0;y<n;y++)dis[y]=(dis[y]>dis[x]+adj[x][y]?dis[x]+adj[x][y]:dis[y]);}if(flag[b])//若a点与b点连通return dis[b];else return -1;}int main(){cin>>t;while(t--){cin>>road;for(int i=0;i<205;i++)for(int j=0;j<205;j++)adj[i][j]=((i==j)?0:INF);map<string,int>mymap;//路与编号相对应n=0;//n记录对应路的编号for(int i=0;i<road;i++){//string end;cin>>start>>send>>d;if(!mymap.count(start))mymap[start]=n++;if(!mymap.count(send))mymap[send]=n++;adj[mymap[start]][mymap[send]]=adj[mymap[send]][mymap[start]]=d;}cin>>start>>send;if(start==send) cout<<0<<endl;else if(!mymap.count(start)||!mymap.count(send)) cout<<-1<<endl;else cout<<dijkstra(mymap[start],mymap[send])<<endl;}return 0;}
- 1031. Campus
- 1031. Campus
- 1031. Campus
- Sicily 1031. Campus
- Sicily 1031. Campus
- [sicily online]1031. Campus
- sicily 1031. Campus
- Sicily 1031. Campus
- Sicily 1031. Campus
- sicily 1031.Campus
- sicily 1031. Campus
- Campus
- Sicilly 1031. Campus (Floyd 算法)
- Sicily 1031. Campus(shortest path)
- Sicily---1031. Campus(最短路径)
- 1031.Campus 求两个地点的最短路径
- sicily 1031. Campus (优先队列优化的dijistra模板)
- In Campus
- 在Linux下编写Daemon
- Linux移植参考手册v1.0
- CentOS 编译 rebar出错
- xcode4.2新建Window_Base_Application的方法 英文版 按照书上做demo的有福了(很简单)
- 黑马程序员——Java动态代理
- 1031. Campus
- xcode生成文件
- Android媒体的一些使用总结
- 人工智能的一点摘抄 《视读 人工智能》
- OpenCV学习笔记(三十九)——再谈OpenCV的数据结构,Mat是如此强大
- 在Rprofile.site文件中设置R的镜像站点
- 最大子序列和的线性时间算法
- 先做个码农
- 超巧妙的设置密码的方法