[HDU-2112] HDU Today (dijkstra single source shortest path + map conversion)

Keywords: Java

Topic:

HDU Today

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 11   Accepted Submission(s) : 5

Problem Description

With the help of the brocade, Haidong Group finally survived the crisis. Since then, the development of HDU has been going smoothly. By 2050, the group has become quite large and is said to have entered the top 500 of Qianjiang Meat Silk Economic Development Zone. At this time, the XHD couple also retired to the second tier, and bought a house in Taoyao Village, Lipu Town, Zhuji City, which has beautiful scenery, and began to spend their old age peacefully.
After living in this way for some time, Xu always did not know much about the local traffic. Sometimes very depressed, want to go to a place and do not know what bus to take, where to transfer, where to get off (in fact, Xu always own a car, but must be happy with the people, this is Xu's character).
Xu always asks poor English for directions: "Can you help me?". Looking at his confused and helpless eyes, can you help him enthusiastically?
Please help him to reach his destination in the shortest possible time (assuming that every bus stops at the starting and terminal stations and will leave at any time).

 

Input

There are many groups of input data, the first line of each group is the total number of buses N (0<=N<=10000).
In the second line, Xu's general location is start and his destination is end.
Then there are n rows, each with station name s, station name e, and the time integer t from s to e (0 < T < 100) (each place name is a string of no more than 30 lengths).
note: The number of place names in a set of data will not exceed 150.
If N==-1, the input ends.

Output

If Xu can always reach the destination, the shortest time to output; otherwise, output "-1".

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

Hint: The best route is: Xiasha - > Shopping Center of Hang Zhou - > supermarket - > westlake, although occasionally lost, but because of your help ** and ** from now on, they still live a happy life. The end of the play

Problem-solving report:

Nearly the shortest path of the single source of bare questions, add one to judge whether the two are achievable. (available and searchable or dis[v] = INF)

The string is map ped to an integer.

AC Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<iostream>
using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
map<string,int> mp; 
struct Node {
	int to;
	int w;
	int ne;
//	bool operator <(const Node & b) const {
//		return w>b.w;
//	}
} e[MAX];

struct point {
	int pos;
	int c;
	point(){}//No node can be written without this constructor
	point(int pos,int c):pos(pos),c(c){}//You can write node(pos,cost) like this
	bool operator <(const point & b) const {
		return c>b.c;
	}
};

int head[MAX];
int vis[MAX];
int cnt = 0;
int n,m;
int top = -1;
int dis[MAX];//Represents the shortest distance from the starting point to that point. 
void add(int u,int v,int w) {
	e[cnt].to = v;
	e[cnt].w = w;
	e[cnt].ne = head[u];
	head[u] = cnt;
	cnt++;
}
void Dijkstra(int u,int v) {
	priority_queue<point> pq; 
	dis[u] = 0;
	point cur = point(u,0);
	pq.push(cur);
//	vis[u] = 1;
	while(!pq.empty()) {
		point now = pq.top();pq.pop();
		if(vis[now.pos] == 1) continue;
		vis[now.pos] = 1;
		if(now.pos == v) break;//This step is not put in if (vis [now. pos]== 1) continue; the reason is that it is easy to record as v, so it is unified. 
		for(int i = head[now.pos]; i!=-1; i=e[i].ne) {
			if(  dis[e[i].to] > dis[now.pos] + e[i].w ) {
				dis[e[i].to] = dis[now.pos] + e[i].w;
				pq.push(point(e[i].to,dis[e[i].to] ) );	
			}
		} 
	}
	if(dis[v] == INF) {
		printf("-1\n");
	}
	else  printf("%d\n",dis[v]);
	
} 
void init() {
	cnt = 0;top = -1;
	mp.clear();
	memset(head,-1,sizeof(head) );
	memset(dis,INF,sizeof(dis) ) ;
	memset(vis,0,sizeof(vis) ) ;
}
int main()
{
	
	int a,b,w;
	char st[40],ed[40];
	char u[40],v[40];
	while(~scanf("%d",&n) ) {
		if(n == -1) break;
		init();
		scanf("%s %s",st,ed);
		if(mp.find(st) == mp.end() ) mp[st] = ++top;
		if(mp.find(ed) == mp.end() ) mp[ed] = ++top;
		for(int i = 1; i<=n; i++) {
			scanf("%s %s %d",&u,&v,&w);
			if(mp.find(u) == mp.end() ) mp[u] = ++top;
			if(mp.find(v) == mp.end() ) mp[v] = ++top;
			
			add(mp[u],mp[v],w);add(mp[v],mp[u],w);
		}
		Dijkstra(mp[st],mp[ed]);//Or Dijkstra(0,1)
	}
	
	return 0 ;
 } 

Posted by oeb on Tue, 11 Dec 2018 13:15:05 -0800