Codevs 2800 tsp problem of sending and selling out

Source: Internet
Author: User

2800 take-out

time limit: 2 sspace limit: 256000 KBtitle level: Diamonds DiamondTitle Description Description

There was a delivery, he had n orders on his hand, and he had to deliver n parts to the hands of n different clients. N different customers are in 1~n numbered cities. Delivery from the city of No. 0, and then N cities to walk once (a city can go many times), and finally back to 0 points (his unit), ask the shortest time is how much. The time for direct access to any two cities is now known.

Enter a description Input Description

First line a positive integer n (1<=n<=15)

Next is a (n+1) * (n+1) matrix in which the number in the matrix is a positive integer that does not exceed 10000. The I row j column of the Matrix represents the time of the direct access between City I-1 and City j-1. Of course, the direct access time of city A to City B and City B to city A is not necessarily the same, that is, the road is one-way.

Output description Output Description

A positive integer representing the minimum time spent

Sample input Sample Input
30 1 10 101 0 1 210 1 0 1010 2 10 0
Sample output Sample Output

8

Data range and Tips Data Size & Hint

1<=n<=15

Idea: Floyd Shortest path + state compression primer;

To die, starting from 1;

#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineESP 0.00000000001Const intn=2e5+Ten, m=1e6+Ten, inf=1e9+Ten, mod=1e9+7;intdp[n][ -];intmp[ -][ -];intMain () {intx,y,z,i,t;  for(i=0; i<n;i++)         for(t=0;t< -; t++) Dp[i][t]=inf; scanf ("%d",&x); X++;  for(i=1; i<=x;i++)         for(t=1; t<=x;t++) scanf ("%d",&mp[i][t]);  for(intK =1; K <= x; k++)         for(inti =1; I <= x; i++)             for(intj =1; J <= X; J + +) Mp[i][j]= Min (Mp[i][j], mp[i][k] +Mp[k][j]); dp[2][1]=0;  for(t=0;t< (1<< (x+1)); t++)    {         for(i=1; i<=x;i++)        {             for(intj=1; j<=x;j++)            if((t& (1<<i))! =0) && (t& (1&LT;&LT;J))! =0)) Dp[t][i]=min (dp[t][i],dp[t^ (1<<i)][j]+Mp[j][i]); }    }    intans=inf;  for(intI=1; i<=x;i++) ans=min (ans,mp[i][1]+dp[(1<< (x+1))-2][i]); printf ("%d\n", ans); return 0;}

Codevs 2800 tsp problem of sending and selling out

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.