3122 dairy agency VIII, 3122 viii

Source: Internet
Author: User

3122 dairy agency VIII, 3122 viii
3122 dairy agency VIII

 

Time Limit: 3 s space limit: 256000 KB title level: Master Title Description Description

Xiao Xu is a dairy agency in USACO China and specializes in selling high-quality and inexpensive "FJ" brand cows.

One day, her cows were sold out and she had to purchase goods in the United States.

She needs to go to N dairy farms to ask about the price (Xiao Xu is a serious person and must shop for three items ).

We will provide you with an adjacent matrix that represents the path length of N farms. (Starting from farm 1, and finally returning to the starting point to buy)

Input description Input Description

N

Adjacent matrix

Output description Output Description

Answer (see description)

Sample Input Sample Input

3

0 1 2

3 0 10

2 0 0

 

Sample output Sample Output

5

Data range and prompt Data Size & Hint

N <= 15, path length <= 1000

TSP

 

CATEGORY tag Tags click here to expand

 

Pressure DP is disgusting ..

There are two key points in this question,

1. Walk through all the points

2. Shortest Path

2nd shortest paths are better solved. If n is less than 16, you can use Floyd once.

But the first condition goes through all points.

We can consider using the state compression method.

We can use a binary string to indicate whether the vertex has passed through. For example, 110 indicates that the vertices 1 and 2 have passed through, and 3rd vertices have not passed through

Code:

Set a dp array. dp [now] [j] indicates the cost of reaching Point j in the now state.

First, we need to enumerate I and j For the shortest distance.

Second, we also need to enumerate a variable now that can capture all States to record each State that can be reached.

When the status now can reach j, it means that we can reach I through this status (there must be a path between I and j)

At last, enumerate each vertex and obtain the minimum value.

 

Details:

1. When running floyd, do not set the maximum value in advance, because each two points (different) must have edge connections.

2. the first digit of the dp array must be large enough. The minimum value is 2 ^ 16, because the first dimension record is State rather than size.

3. now <= (1 <n)-1:

When n = 3, 1 <3 = 2 ^ 3 = 8 = 1000

1000-1 = 111 is the ideal situation when all three points arrive.

4. now & (1 <(J-1 ))

The J-1 is to not exceed the boundary and enumerate all the situations

First of all, it should be clear that 1 <(J-1) to obtain a number of 2 ^ x, the conversion to binary must be 1 + 000... form

So when now & (1 <(J-1) has a value, it is said that the state now can reach j point

5. now | (1 <(I-1 ))

In this operation, now must meet now & (1 <(J-1 ))! = 0 (the program is executed in sequence)

Now, the status can reach j.

And 1 <(I-1) must be a number of 2 ^ x

So now | (1 <(I-1) is a new State that can be generated by j to reach I

Example:

Now = 110010

I = 100.

The result is 110110.

 

Code:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=30; 7 int read(int & n) 8 { 9     char p='+';int x=0;10     while(p<'0'||p>'9')11         p=getchar();12     while(p>='0'&&p<='9')13     x=x*10+p-48,p=getchar();14     n=x;15 }16 int dp[MAXN*2000][MAXN];17 int dis[MAXN][MAXN];18 int n;19 void floyed()20 {21     for(int i=1;i<=n;i++)22         for(int j=1;j<=n;j++)23             for(int k=1;k<=n;k++)24                 if(i!=j&&j!=k&&i!=k)25                     dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);26 }27 void zhuangya()28 {29     /*for(int i=1;i<=n;i++)30     {31         for(int j=1;j<=n;j++)32         {33             cout<<dis[i][j]<<" ";34         }35         cout<<endl;36     }*/37     memset(dp,0xf,sizeof(dp));38     dp[1][1]=0;39     for(int now=0;now<=(1<<n)-1;now++)40         for(int i=1;i<=n;i++)41             for(int j=1;j<=n;j++)42                 if((now&(1<<(j-1)))&&i!=j)43                 {44                     dp[now|(1<<(i-1))][i]=45                         min46                         (47                             dp[now|(1<<(i-1))][i],48                             dp[now][j]+dis[j][i]49                         );50                 }51     int ans=0x7ffffff;52     for(int i=2;i<=n;i++)53     {54         ans=min(ans,dp[(1<<n)-1][i]+dis[i][1]);55     }56     cout<<ans;57 }58 int main()59 {60     read(n);61     for(int i=1;i<=n;i++)62         for(int j=1;j<=n;j++)63             read(dis[i][j]);64     floyed();65     zhuangya();66     return 0;67 }

 

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.