Einbahnstrasse
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3735 Accepted Submission(s): 1184
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.
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.
k . V
Where k is test case number (starting at 1,) is a space, and V is the result.
题意:求破车运到修理站且从修理站运回破车原来所在地点的最短距离
思路:这也是一个多源点到单源点的题目,用一个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;
}