博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
joisino's travel
阅读量:6273 次
发布时间:2019-06-22

本文共 5625 字,大约阅读时间需要 18 分钟。

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 Rr1 … rRA1 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 31 2 31 2 12 3 13 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 21 32 3 21 3 61 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 32 3 41 2 42 3 34 3 11 4 14 2 23 1 6

Sample Output 3

Copy

3

 

 

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

 

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

1 # include 
2 # include
3 # include
4 # include
5 # include
6 # include
7 # include
8 # include
9 # include
10 # include
11 # include
12 # include
13 # include
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]
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 
2 # include
3 # include
4 # include
5 # include
6 # include
7 # include
8 # include
9 # include
10 # include
11 # include
12 # include
13 # include
14 using namespace std;15 # define LL long long16 # define pr pair17 # define mkp make_pair18 # define lowbit(x) ((x)&(-x))19 # define PI acos(-1.0)20 # define INF 0x3f3f3f3f21 # define eps 1e-822 # define MOD 100000000723 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 20536 /**************************/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<
View Code

 

 

转载于:https://www.cnblogs.com/haoabcd2010/p/7516011.html

你可能感兴趣的文章
ireport报表自定义字体
查看>>
人为触发jquery ajax 中error回调方法
查看>>
JavaScript 基础(九): 条件 语句
查看>>
Linux系统固定IP配置
查看>>
jQuery-强大的jQuery选择器 (详解)[转]
查看>>
配置Quartz
查看>>
Linux 线程实现机制分析
查看>>
继承自ActionBarActivity的activity的activity theme问题
查看>>
设计模式01:简单工厂模式
查看>>
多线程讲解
查看>>
项目经理笔记一
查看>>
Hibernate一对一外键双向关联
查看>>
mac pro 入手,php环境配置总结
查看>>
MyBatis-Plus | 最简单的查询操作教程(Lambda)
查看>>
rpmfusion 的国内大学 NEU 源配置
查看>>
spring jpa 配置详解
查看>>
IOE,为什么去IOE?
查看>>
java 用反射简单应用,将Object简单转换成map
查看>>
Storm中的Worker
查看>>
dangdang.ddframe.job中页面修改表达式后进行检查
查看>>