UVA-11280 Flying to Fredericton (two-dimensional SPFA)

Source: Internet
Author: User
Tags bool cas min printf


link:http://acm.hust.edu.cn/vjudge/problem/viewproblem.action?id=25133



Description problem C FLYING to Fredericton

After being inspired by Salvador Dali's artwork, Brett decided he would like-to-travel-to-Fredericton, New Brunswick to VI Sit the Beaverbrook Art Gallery.

Brett lives in Calgary, and would like to find the cheapest flight or flights that would take him to Fredericton. He knows that a direct flight from Calgary to Fredericton, if one exists, would being absurdly expensive, so he's willing to Tolerate a certain number of stopovers. However, there is multiple airlines in Canada with so many different flights between different cities now, which makes it Very difficult for Brett to find the least expensive-to fredericton! Can you write a program to help Brett plan his route?

Map showing a sample of the flights that would take Brett to Fredericton.

You'll be given a list of cities between and including Calgary and Fredericton. The cities is given in order of "nearest" to "farthest". The first city is always being "Calgary" and the Last "Fredericton".

You'll also be given a list of flights between pairs of cities, and the associated cost for each flight, taxes included. There'll never be a flight from a farther city to a nearer city-brett have already discarded those flights, deeming th Em to is a waste of time and money. Bear on mind, however, so there may are more than one flight between any and cities, as Brett is considering flights from All airlines.

Finally, you is presented with a number of queries. Each query was a single integer indicating the maximum number of stopovers Brett would tolerate. For each query, your program must calculate the least total cost of flights that would take Brett from Calgary to Frederic ton with no more than the requested number of stopovers. Input

The first line of input contains a, indicating the number of scenarios to process. A blank line precedes each scenario.

Each scenario begins with a number N (2≤n≤100), the number of cities, followed by N lines containing the names of the Cities. A city name was a string of up to uppercase and lowercase letters. Next is a number M (0≤m≤1000) and the number of flights available, followed by M lines describing the flights. Each flight was described by its departure city, its destination city, and an integer representing it cost in dollars. The final line starts with Q (1≤Q≤10), the number of queries, followed by Q numbers indicating the maximum number of S Topovers. Output

For each scenario, your program should output the scenario number, followed by the least total cost of the flights for EAC H query. Follow the format of the sample output. If no flights can satisfy a query, write "no satisfactory flights". Output a blank line between scenarios. Sample Input

2

4
Calgary
Winnipeg
Ottawa
Fredericton
6
Calgary Winnipeg
300 Calgary Ottawa
Winnipeg Fredericton 325
Winnipeg Ottawa
Calgary Fredericton 875 Ottawa
Fredericton 175
3 2 1 0

3
Calgary
Montreal
Fredericton
2
Calgary Montreal Montreal Fredericton 325
1 0
Output for the Sample Input
Scenario #1 Total cost of
flight (s) is $
flight (s) is $450 total cost of
flight (s) is $875

Scenario #2
No satisfactory flights
Brett Shikaze
Calgary Collegiate Programming Contest 2006


Test instructions: Give the N City and M route, each route connects two cities, one is the departure city of the route, one is the arrival city of the route, and has the corresponding cost. Given the starting point and destination city, then there are Q queries, each asking for the maximum number of cities that can pass between the starting point and the destination city route, and ask how much the minimum cost of the city to the destination city is from the specified starting point to the city, if the number of cities passing through is less than or equal to the maximum number of cities. If the destination cannot be reached under this condition, output "No satisfactory flights".


Problem-solving idea: To use two-dimensional to represent the state. One-dimensional means the shortest path to a city (that is, the cost of the topic), and the second dimension represents the number of cities passing by in the middle of a city. At the same time the question input data is troublesome, to use some techniques to the input data processing, but also pay attention to the query input data may not be legal, this is a big pit point, to the data after processing. The rest is the bare spfa.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < queue> #include <vector> #include <map> #include <cmath> #define LL Long long #define MAXN 1000010 usin
G namespace Std;
const int inf=0x3f3f3f3f;
int N,m,tot;
int HEAD[MAXN];
	struct state{//uses a struct to represent the current state: The city is currently reached, the number of cities that have stopped is stop int city;
int stop;
};
	struct node{int from;
	int to;
	int W;
int next;
}EDGE[MAXN];
int dis[111][111];
BOOL inq[111][111];
int cnt[111][111];
	void Init () {memset (head,-1,sizeof (head));
tot=0;
	} void Add (int from,int to,int W) {edge[tot].from=from;
	Edge[tot].to=to;
	Edge[tot].w=w;
	Edge[tot].next=head[from];
head[from]=tot++;
	} bool SPFA (int start,int ed) {deque<state>q;
	memset (dis,inf,sizeof (dis));
	memset (inq,false,sizeof (INQ));
	memset (cnt,0,sizeof (CNT));
	State St;
	St.city=start;
	St.stop=0;
	dis[st.city][st.stop]=0;
	Q.push_back (ST);
	Inq[st.city][st.stop]=true;
	cnt[st.city][st.stop]++; while (!q.empty ()) {State now=q.front ();
		Q.pop_front ();
		Inq[now.city][now.stop]=false;
		if (cnt[now.city][now.stop]>n) {continue;
		} if (cnt[now.city][now.stop]==n) {dis[now.city][now.stop]=inf;
			} for (int. I=head[now.city];i!=-1;i=edge[i].next) {State NEX;
			nex.city=edge[i].to;
			nex.stop=now.stop+1; if (DIS[NEX.CITY][NEX.STOP]&GT;DIS[NOW.CITY][NOW.STOP]+EDGE[I].W) {dis[nex.city][nex.stop]=dis[now.city][
				NOW.STOP]+EDGE[I].W;
					if (!inq[nex.city][nex.stop]) {State front=q.front ();
					if (!q.empty () &&dis[nex.city][nex.stop]<dis[front.city][front.stop]) {Q.push_front (NEX);
					} else {q.push_back (NEX);
}}}}} return true;
	} int main () {//freopen ("D:\in.txt", "R", stdin);
	int T,cas,i,j,u,v,w,q,s,ans;
	scanf ("%d", &t);
		for (cas=1;cas<=t;cas++) {scanf ("%d", &n);
		map<string,int>city;
		String name1,name2; for (i=1;i<=n;i++) {cin>>name1;//A big pit point, the input is not the city number, is the cityName, use map to cleverly city[name1]=i;//the city name of the string into the city number of the numeric type} scanf ("%d", &m);
		Init ();
			while (m--) {cin>>name1>>name2>>w;
		Add (CITY[NAME1],CITY[NAME2],W);
		} int start=city["Calgary"];
		int ed=city["Fredericton"];
		SPFA (start,ed);
		scanf ("%d", &q);
		printf ("Scenario #%d\n", CAs);
			while (q--) {scanf ("%d", &s); S=min (s,n-2);//A big pit point ... 
			The number of possible stops entered may be outside the legal range and must be handled by yourself ...
			Ans=inf;
			for (i=0;i<=s;i++) ans=min (ans,dis[n][i+1]);
			if (ans>=inf) {printf ("No satisfactory flights\n");
			} else {printf ("Total cost of flight (s) is $%d\n", ans);
		}} if (cas!=t) {printf ("\ n");
 }} return 0;  }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.