joisino's travel

D - joisino's travel


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town Ai and Bi and has a length of Ci.

Joisino is visiting R towns in the state, r1,r2,..,rR (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

  • 2≤N≤200
  • 1≤MN×(N−1)⁄2
  • 2≤Rmin(8,N) (min(8,N) is the smaller of 8 and N.)
  • rirj(ij)
  • 1≤Ai,BiN,AiBi
  • (Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(ij)
  • 1≤Ci≤100000
  • Every town can be reached from every town by road.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r1  rR
A1 B1 C1
:
AM BM CM

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.


Sample Input 1

Copy
3 3 3
1 2 3
1 2 1
2 3 1
3 1 4

Sample Output 1

Copy
2

For example, if she visits the towns in the order of 123, the distance traveled will be 2, which is the minimum possible.


Sample Input 2

Copy
3 3 2
1 3
2 3 2
1 3 6
1 2 2

Sample Output 2

Copy
4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.


Sample Input 3

Copy
4 6 3
2 3 4
1 2 4
2 3 3
4 3 1
1 4 1
4 2 2
3 1 6

Sample Output 3

Copy

3

 

 

//题意,n 个点, m 条边,,R 个需要去的地方,可以从任意一点出发,终于任意一点,但必须走完 R 个点,问最小路径为多少?

 

//首先,用Floyd求出最短路,然后暴力枚举要走的点的顺序即可 8!也就1千万

  1 # include <cstdio>
  2 # include <cstring>
  3 # include <cstdlib>
  4 # include <iostream>
  5 # include <vector>
  6 # include <queue>
  7 # include <stack>
  8 # include <map>
  9 # include <bitset>
 10 # include <sstream>
 11 # include <set>
 12 # include <cmath>
 13 # include <algorithm>
 14 # pragma  comment(linker,"/STACK:102400000,102400000")
 15 using namespace std;
 16 # define LL          long long
 17 # define pr          pair
 18 # define mkp         make_pair
 19 # define lowbit(x)   ((x)&(-x))
 20 # define PI          acos(-1.0)
 21 # define INF         0x3f3f3f3f3f3f3f3f
 22 # define eps         1e-8
 23 # define MOD         1000000007
 24  
 25 inline int scan() {
 26     int x=0,f=1; char ch=getchar();
 27     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
 28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
 29     return x*f;
 30 }
 31 inline void Out(int a) {
 32     if(a<0) {putchar('-'); a=-a;}
 33     if(a>=10) Out(a/10);
 34     putchar(a%10+'0');
 35 }
 36 # define MX 205
 37 /**************************/
 38  
 39 int n,m,R;
 40 int ans;
 41 int goal[MX];
 42 int mp[MX][MX];
 43 int G[10][10];
 44  
 45 void floyd()
 46 {
 47     for (int k=1;k<=n;k++)
 48         for (int i=1;i<=n;i++)
 49             for (int j=1;j<=n;j++)
 50                 if (mp[i][j]>mp[i][k]+mp[k][j])
 51                     mp[i][j] = mp[i][k]+mp[k][j];
 52 }
 53  
 54 int prim()
 55 {
 56     int dis[10];
 57     int vis[10];
 58     memset(dis,0x3f,sizeof(dis));
 59     memset(vis,0,sizeof(vis));
 60     dis[1] = 0;
 61  
 62     int all = 0;
 63     for (int i=1;i<=R;i++)
 64     {
 65         int dex, mmm = INF;
 66         for (int j=1;j<=R;j++)
 67         {
 68             if (!vis[j]&&dis[j]<mmm)
 69             {
 70                 mmm=dis[j];
 71                 dex=j;
 72             }
 73         }
 74         all += dis[dex];
 75         vis[dex]=1;
 76         for (int j=1;j<=R;j++)
 77         {
 78             if (!vis[j]&&dis[j]>G[dex][j])
 79                 dis[j]=G[dex][j];
 80         }
 81     }
 82     return all;
 83 }
 84  
 85 int vis[10];
 86 void dfs(int s,int pos,int far)
 87 {
 88     if (s>R)
 89     {
 90         ans = min(ans,far);
 91         return ;
 92     }
 93     for (int i=1;i<=R;i++)
 94     {
 95         if (vis[i]) continue;
 96         vis[i] = 1;
 97         dfs(s+1,i,far+G[pos][i]);
 98         vis[i]=0;
 99     }
100 }
101  
102 int main()
103 {
104     scanf("%d%d%d",&n,&m,&R);
105  
106     for (int i=1;i<=R;i++)
107         goal[i] = scan();
108  
109     memset(mp,0x3f,sizeof(mp));
110     for (int i=1;i<=m;i++)
111     {
112         int a,b,c;
113         scanf("%d%d%d",&a,&b,&c);
114         mp[a][b] = mp[b][a] = c;
115     }
116  
117     floyd();
118     for (int i=1;i<=R;i++)
119         for (int j=1;j<=R;j++)
120             G[i][j] = mp[ goal[i] ][ goal[j] ];
121  
122     ans = INF;
123     for (int i=1;i<=R;i++)
124     {
125         vis[i]=1;
126         dfs(2,i,0);
127         vis[i]=0;
128     }
129     printf("%d\n",ans);
130 }
View Code

 

//如果R再大点(16左右),还可以状态压缩一下 dp[i][j] 表在 i 点,状态为 j 时的最小路径

 1 # include <cstdio>
 2 # include <cstring>
 3 # include <cstdlib>
 4 # include <iostream>
 5 # include <vector>
 6 # include <queue>
 7 # include <stack>
 8 # include <map>
 9 # include <bitset>
10 # include <sstream>
11 # include <set>
12 # include <cmath>
13 # include <algorithm>
14 using namespace std;
15 # define LL          long long
16 # define pr          pair
17 # define mkp         make_pair
18 # define lowbit(x)   ((x)&(-x))
19 # define PI          acos(-1.0)
20 # define INF         0x3f3f3f3f
21 # define eps         1e-8
22 # define MOD         1000000007
23 
24 inline int scan() {
25     int x=0,f=1; char ch=getchar();
26     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
27     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
28     return x*f;
29 }
30 inline void Out(int a) {
31     if(a<0) {putchar('-'); a=-a;}
32     if(a>=10) Out(a/10);
33     putchar(a%10+'0');
34 }
35 # define MX 205
36 /**************************/
37 
38 int n,m,R;
39 int ans;
40 int goal[MX];
41 int mp[MX][MX];
42 int G[25][25];
43 int dp[16][(1<<16)];
44 
45 void floyd()
46 {
47     for (int k=1;k<=n;k++)
48         for (int i=1;i<=n;i++)
49             for (int j=1;j<=n;j++)
50                 if (mp[i][j]>mp[i][k]+mp[k][j])
51                     mp[i][j] = mp[i][k]+mp[k][j];
52 }
53 
54 int main()
55 {
56     scanf("%d%d%d",&n,&m,&R);
57     for (int i=1;i<=R;i++)
58         goal[i] = scan();
59     memset(mp,0x3f,sizeof(mp));
60     for (int i=1;i<=m;i++)
61     {
62         int a,b,c;
63         scanf("%d%d%d",&a,&b,&c);
64         mp[a][b] = mp[b][a] = c;
65     }
66 
67     floyd();
68     for (int i=1;i<=R;i++)
69         for (int j=1;j<=R;j++)
70             G[i][j] = mp[ goal[i] ][ goal[j] ];
71 
72     memset(dp,0x3f,sizeof(dp));
73     for (int i=1;i<=R;i++)
74         dp[i-1][(1<<(i-1))] = 0;
75 
76     for (int i=0;i<(1<<R);i++) //sta
77         for (int j=1;j<=R;j++)
78             if ((1<<(j-1))&i) //qi
79                 for (int k=1;k<=R;k++) //zhong
80                     dp[k-1][i|(1<<(k-1))]=min(dp[k-1][i|(1<<(k-1))],dp[j-1][i]+G[j][k]);
81 
82     int ans = INF;
83     for (int i=1;i<=R;i++)
84         ans=min(ans,dp[i-1][(1<<R)-1]);
85     printf("%d\n",ans);
86 }
View Code

 

 

posted @ 2017-09-13 17:14  happy_codes  阅读(150)  评论(0编辑  收藏  举报