Topic Link http://poj.org/problem?id=3311
Hie with the Pie
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 5094 |
|
Accepted: 2716 |
Description
The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He'll wait for 1 or more (up to ten) orders to being processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even If it means passing the same location (s) or the pizzeria more than once on the. He has the commissioned you-to-write a program-to-help him.
Input
Input would consist of multiple test cases. The first line would contain a single integerNindicating the number of orders to deliver, where 1≤N≤10. After this'll beN+ 1 lines each containingN+ 1 integers indicating the times to travel between the pizzeria (numbered 0) and theNLocations (Numbers 1 toN). TheJTh value on theITh line indicates the time-to-go directly from locationITo locationJWithout visiting any other locations along the. Note that there is quicker ways to go fromIToJVia other locations, due to different speed limits, traffic lights, etc. Also, the time values may is symmetric, i.e., the time to go directly from locationIToJSame as the time to go directlyJToI. An input value ofN= 0 would terminate input.
Output
For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.
Sample Input
30 1 10 101 0 1 210 1 0 1010 2 10 00
Sample Output
8
Source
The problem of traveling salesman (traveling salesman problem,tsp) is also translated into the problem of traveling salesmen, traveling salesman problem, referred to as TSP, is the most basic route problem, the problem is to seek a single traveler from the starting point, through all the given demand point, The least-path cost of returning to the origin at a later point.
Test instructions: A person who transports pizza, departs from a pizzeria, needs to be transported to every customer ordered, and then back to the pizzeria to find out how much time it takes. (each point can be repeated walk)
The first line of the topic gives an n, which means there are several places to ship, followed by a matrix A[i][j] that represents the time it takes to take from I to J. 0 means pizza shop. , because it can be repeated, so the first time with the Freud algorithm to find the shortest path.
Analysis: The first idea is also a pressure DP (here n max = 10, which can also be enumerated), but I didn't think about how to represent the state at first. Only Dp[i] indicates the state.
In fact should be set DP[I][J] indicates the time at which the end point is the minimum of J when the state is I.
This solution goes back to the point where it takes min (dp[(1<<n) -1][1]+a[1][0], dp[((1<<n) -1][2]+a[2][0] +....+dp[((1<<n) -1][n]+ A[n][0])
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <string>5#include <algorithm>6 using namespacestd;7 #defineINF 0x3f3f3f8 intN;9 intmp[ One][ One];Ten intdp[1<< One][ One]; One intMain () { A while(SCANF ("%d", &n) &&N) { - for(inti =0; I <= N; i++){ - for(intj =0; J <= N; J + +){ thescanf"%d", &mp[i][j]); - } - } - for(inti =0; I <= N; i++){ + for(intj =0; J <= N; J + +){ - for(intK =0; K <= N; k++){ + if(Mp[i][j] + mp[j][k] <= mp[i][k]) mp[i][k] = Mp[i][j] +Mp[j][k]; A } at } -}//The floyed algorithm is preprocessed first. - - for(inti =0; I < (1<<N); i++){ - for(intj =0; J <= N; J + +) dp[i][j] =-1; - } indp[0][0] =0; - for(intpos =1; POS <= N; pos++){ todp[1<< (pos-1)][pos] = mp[0][pos]; + } - the * for(inti =0; I < (1<<N); i++){ $ for(intj =0; J <= N; J + +){Panax Notoginseng if(Dp[i][j]! =-1){//This state can be achieved - for(intK =0; K <= N; k++){ the if(( (1<< (K-1)) &i)! =0)Continue;//If this point has passed, + if(Dp[i| (1<< (K-1))][k] = =-1) dp[i| (1<< (K-1))][k] = Dp[i][j] +Mp[j][k]; A Else if(Dp[i| (1<< (K-1))][k] >= Dp[i][j] + mp[j][k]) dp[i| (1<< (K-1))][k]= Dp[i][j] +Mp[j][k]; the } + } - } $ } $ intMin =INF; - for(inti =1; I <= N; i++){ - if(Dp[(1<<n)-1][i] + mp[i][0] <= min) min = dp[(1<<n)-1][i] + mp[i][0]; the } -printf"%d\n", Min);Wuyi } the - return 0; Wu}
poj3311 Hie with the Pie travel salesman problem (TSP)