HDU 2923 Einbahnstrasse(最短路徑,多源點到單源點)

Einbahnstrasse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3735    Accepted Submission(s): 1184


Problem Description
Einbahnstra  e (German for a one-way street) is a street on which vehicles should only move in one direction. One reason for having one-way streets is to facilitate a smoother flow of traffic through crowded areas. This is useful in city centers, especially old cities like Cairo and Damascus. Careful planning guarantees that you can get to any location starting from any point. Nevertheless, drivers must carefully plan their route in order to avoid prolonging their trip due to one-way streets. Experienced drivers know that there are multiple paths to travel between any two locations. Not only that, there might be multiple roads between the same two locations. Knowing the shortest way between any two locations is a must! This is even more important when driving vehicles that are hard to maneuver (garbage trucks, towing trucks, etc.)

You just started a new job at a car-towing company. The company has a number of towing trucks parked at the company's garage. A tow-truck lifts the front or back wheels of a broken car in order to pull it straight back to the company's garage. You receive calls from various parts of the city about broken cars that need to be towed. The cars have to be towed in the same order as you receive the calls. Your job is to advise the tow-truck drivers regarding the shortest way in order to collect all broken cars back in to the company's garage. At the end of the day, you have to report to the management the total distance traveled by the trucks.
 

Input
Your program will be tested on one or more test cases. The first line of each test case specifies three numbers (N , C , and R ) separated by one or more spaces. The city has N locations with distinct names, including the company's garage. C is the number of broken cars. R is the number of roads in the city. Note that 0 < N < 100 , 0<=C < 1000 , and R < 10000 . The second line is made of C + 1 words, the first being the location of the company's garage, and the rest being the locations of the broken cars. A location is a word made of 10 letters or less. Letter case is significant. After the second line, there will be exactly R lines, each describing a road. A road is described using one of these three formats:


A -v -> B
A <-v - B
A <-v -> B


A and B are names of two different locations, while v is a positive integer (not exceeding 1000) denoting the length of the road. The first format specifies a one-way street from location A to B , the second specifies a one-way street from B to A , while the last specifies a two-way street between them. A , ``the arrow", and B are separated by one or more spaces. The end of the test cases is specified with a line having three zeros (for N , C , and R .)

The test case in the example below is the same as the one in the figure. 

 

Output
For each test case, print the total distance traveled using the following format:


k . V


Where k is test case number (starting at 1,) is a space, and V is the result.
 

Sample Input
4 2 5 NewTroy Midvale Metrodale NewTroy <-20-> Midvale Midvale --50-> Bakerline NewTroy <-5-- Bakerline Metrodale <-30-> NewTroy Metrodale --5-> Bakerline 0 0 0
 

Sample Output
1. 80

題意:求破車運到修理站且從修理站運回破車原來所在地點的最短距離

思路:這也是一個多源點到單源點的題目,用一個coun數組來計算破車在某個城市出現的次數,我的做法是用dijkstra來做的,因爲用vector來做,所以代碼長了點。如果用結構體仿造鏈表來做,代碼會少很多。因爲給出的n最大不超過100,故又可以用floyd來做(注意重邊和破車位置在修理站的情況),我寫的是dijkstra

上代碼

#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define Max 110
int n,d[Max],d1[Max],cnt,coun[Max],sum;
bool visit[Max];
char str[1005][12];
char start[12],st[12],en[12],len[12],strc[12];
vector<pair<int,int> >G[Max];
vector<pair<int,int> >fG[Max];
void init()
{
	sum=0;
	cnt=0;
	memset(coun,0,sizeof(coun));
	memset(visit,false,sizeof(visit));
	for(int i=0;i<=n;i++)
	{	
		d[i]=inf;
        d1[i]=inf;
	}
}
int pipei(char ss[])
{
	for(int i=0;i<cnt;i++)
	{
		if(strcmp(str[i],ss)==0)
			return i;
	}
	strcpy(str[cnt],ss);
	return cnt++;
}
void dijkstra(int begin)
{
	priority_queue<pair<int,int> >PQ;
	d[begin]=0;
	PQ.push(make_pair(0,begin));
	while(!PQ.empty())
	{
		pair<int,int>f=PQ.top();
		PQ.pop();
		int u=f.second;
		if(d[u]<f.first*(-1))
			continue;
		visit[u]=true;
		for(int i=0;i<G[u].size();i++)
		{
			int v=G[u][i].first;
			if(visit[v])
				continue;
			if(d[v]>d[u]+G[u][i].second)
			{
				d[v]=d[u]+G[u][i].second;
				PQ.push(make_pair(d[v]*(-1),v));
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		if(d[i]!=inf)
			sum=sum+(coun[i]*d[i]);
	}
}
void fdijkstra(int begin)
{
	memset(visit,false,sizeof(visit));
	priority_queue<pair<int,int> >PQ;
	d1[begin]=0;
	PQ.push(make_pair(0,begin));
	while(!PQ.empty())
	{
		pair<int,int>f=PQ.top();
		PQ.pop();
		int u=f.second;
		if(d1[u]<f.first*(-1))
			continue;
		visit[u]=true;
		for(int i=0;i<fG[u].size();i++)
		{
			int v=fG[u][i].first;
			if(visit[v])
				continue;
			if(d1[v]>d1[u]+fG[u][i].second)
			{
				d1[v]=d1[u]+fG[u][i].second;
				PQ.push(make_pair(d1[v]*(-1),v));
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		if(d1[i]!=inf)
			sum=sum+(coun[i]*d1[i]);
	}
}
int main()
{
	//freopen("Text.txt","r",stdin);
	int c,r,k=1;
	while(scanf("%d%d%d",&n,&c,&r)!=EOF)
	{
		if(n==0)
			break;
		init();
		scanf("%s",start);
		for(int i=1;i<=c;i++)
		{
			scanf("%s",strc);
			int p=pipei(strc);
			coun[p]++;
		}
		for(int i=1;i<=r;i++)
		{
			scanf("%s%s%s",st,len,en);
			int u=pipei(st);
			int v=pipei(en);
			int d=0,t=10,uu=0,vv=0;
			int slen=strlen(len);
			for(int j=0;j<slen;j++)
			{
				if(len[j]=='-')
					continue;
				else if(len[j]=='>')
					uu=1;
				else if(len[j]=='<')
					vv=1;
				else if(len[j]>='0'&&len[j]<='9')
				{
					d=d*t+(len[j]-'0');
				}
			}
			if(uu==1&&vv==1)
			{
				G[u].push_back(make_pair(v,d));
				G[v].push_back(make_pair(u,d));
				fG[u].push_back(make_pair(v,d));
				fG[v].push_back(make_pair(u,d));
			}
			else if(uu==1)
			{
				G[u].push_back(make_pair(v,d));
				fG[v].push_back(make_pair(u,d));
			}
			else
			{
				G[v].push_back(make_pair(u,d));
				fG[u].push_back(make_pair(v,d));
			}
		}
		int begin=pipei(start);
		dijkstra(begin);
		fdijkstra(begin);
		printf("%d. %d\n",k++,sum);
		for(int i=0;i<n;i++)
		{
			G[i].clear();
			fG[i].clear();
		}
	}
	return 0;
}


發佈了26 篇原創文章 · 獲贊 0 · 訪問量 3637
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章