hdu2923—Einbahnstrasse(spfa)
程序员文章站
2024-03-17 10:01:46
...
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2923
Einbahnstrasse
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3591 Accepted Submission(s): 1133
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.
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.
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.
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
题目大意:给你n个地点,m个破车,问一拖车从公司出发,将所有破车拖回公司所需最短时间。(又读错了题,以为m个破车在m个不同的地方,结果是m个破车在n个地点)
解题思路:由于题目n个地点只有100,可以直接写个floyed,计算从公司到m个破车的最短距离+m个破车到公司的最短距离。
但由于读错了题,写了个spfa。先从正向建图,跑spfa,计算从公司到m个破车的最短距离,再反向建图,跑spfa,计算从m个破车到公司的最短距离。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <stack>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ull;
const int N = 2000;
const int M = 10800;
const int INF = 0x3fffffff;
const double Pi = acos(-1.0);
struct edge{
int u,v,len;
}e[M*2];
struct Edge{
int node,len;
Edge*next;
}m_edge[M*2];
Edge*head[N];
int Ecnt,dist[N],vis[N];
void init()
{
Ecnt = 0;
fill( head , head+N , (Edge*)0 );
}
void mkEdge( int a , int b , int c )
{
m_edge[Ecnt].node = b;
m_edge[Ecnt].len = c;
m_edge[Ecnt].next = head[a];
head[a] = m_edge+Ecnt++;
}
void spfa( int n )
{
fill( dist , dist+N , INF );
fill( vis , vis+N , 0 );
queue<int>point;
point.push(n);
vis[n] = 1;
dist[n] = 0;
while( !point.empty() ){
int s = point.front();
point.pop();
vis[s] = 0;
for( Edge*p = head[s] ; p ; p = p->next ){
int t = p->node;
if( dist[t] > dist[s]+p->len ){
dist[t] = dist[s]+p->len;
if( !vis[t] ){
point.push(t);
vis[t] = 1;
}
}
}
}
}
int toInt( string a )
{
int cnt = 1,ans = 0;
for( int i = a.length()-1 ; i >= 0 ; --i ){
if( a[i] >= '0' && a[i] <= '9' ){
ans += (a[i]-'0')*cnt;
cnt *= 10;
}
}
return ans;
}
int main()
{
int N,C,R,cnt = 0;
while( ~scanf("%d%d%d",&N,&C,&R) ){
int tot = 0;
if( N == 0 && C == 0 && R == 0 ) break;
string st1,st2,st3;
map<string,int>mp;
vector<string>st(M);
for( int i = 0 ; i < C+1 ; ++i ){
cin >> st[i];
}
int re = 1;
for( int i = 0 ; i < R ; ++i ){
cin >> st1 >> st2 >> st3;
if( !mp[st1] ) mp[st1] = re++;
if( !mp[st3] ) mp[st3] = re++;
int w = toInt(st2);
if( st2[0] == '<' ){
e[tot].u = mp[st3];
e[tot].v = mp[st1];
e[tot].len = w;
++tot;
}
if( st2[st2.length()-1] == '>' ){
e[tot].u = mp[st1];
e[tot].v = mp[st3];
e[tot].len = w;
++tot;
}
}
int ans = 0;
init();
for( int i = 0 ; i < tot ; ++i ){
mkEdge( e[i].u , e[i].v , e[i].len );
}
spfa(mp[st[0]]);
for( int i = 0 ; i < C+1 ; ++i ){
ans += dist[mp[st[i]]];
}
init();
for( int i = 0 ; i < tot ; ++i ){
mkEdge( e[i].v , e[i].u , e[i].len );
}
spfa(mp[st[0]]);
for( int i = 0 ; i < C+1 ; ++i ){
ans += dist[mp[st[i]]];
}
printf("%d. %d\n",++cnt,ans);
}
return 0;
}
推荐阅读
-
HDU 2923 Einbahnstrasse(最短路径,多源点到单源点)
-
HDU 2923 Einbahnstrasse 【Floyd+map】
-
Einbahnstrasse HDU - 2923 (SPFA最短路双向)
-
hdu2923—Einbahnstrasse(spfa)
-
P3371 【模板】单源最短路径(弱化版)(C++_SPFA算法_链式向前星)
-
HDU 1317 XYZZY SPFA
-
ICPC2017网络赛(乌鲁木齐)H: Skiing (SPFA最长路)
-
P3371 【模板】单源最短路径(弱化版)(spfa模板)
-
【单源最短路 spfa Dijkstra】 洛谷 P3371 【模板】单源最短路径(弱化版) ## lguou
-
【图论】【SPFA】单源最短路径(弱化版)