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千万

 # include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
# define MX
/**************************/ int n,m,R;
int ans;
int goal[MX];
int mp[MX][MX];
int G[][]; void floyd()
{
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (mp[i][j]>mp[i][k]+mp[k][j])
mp[i][j] = mp[i][k]+mp[k][j];
} int prim()
{
int dis[];
int vis[];
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
dis[] = ; int all = ;
for (int i=;i<=R;i++)
{
int dex, mmm = INF;
for (int j=;j<=R;j++)
{
if (!vis[j]&&dis[j]<mmm)
{
mmm=dis[j];
dex=j;
}
}
all += dis[dex];
vis[dex]=;
for (int j=;j<=R;j++)
{
if (!vis[j]&&dis[j]>G[dex][j])
dis[j]=G[dex][j];
}
}
return all;
} int vis[];
void dfs(int s,int pos,int far)
{
if (s>R)
{
ans = min(ans,far);
return ;
}
for (int i=;i<=R;i++)
{
if (vis[i]) continue;
vis[i] = ;
dfs(s+,i,far+G[pos][i]);
vis[i]=;
}
} int main()
{
scanf("%d%d%d",&n,&m,&R); for (int i=;i<=R;i++)
goal[i] = scan(); memset(mp,0x3f,sizeof(mp));
for (int i=;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mp[a][b] = mp[b][a] = c;
} floyd();
for (int i=;i<=R;i++)
for (int j=;j<=R;j++)
G[i][j] = mp[ goal[i] ][ goal[j] ]; ans = INF;
for (int i=;i<=R;i++)
{
vis[i]=;
dfs(,i,);
vis[i]=;
}
printf("%d\n",ans);
}

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

 # include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
# define MX
/**************************/ int n,m,R;
int ans;
int goal[MX];
int mp[MX][MX];
int G[][];
int dp[][(<<)]; void floyd()
{
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (mp[i][j]>mp[i][k]+mp[k][j])
mp[i][j] = mp[i][k]+mp[k][j];
} int main()
{
scanf("%d%d%d",&n,&m,&R);
for (int i=;i<=R;i++)
goal[i] = scan();
memset(mp,0x3f,sizeof(mp));
for (int i=;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mp[a][b] = mp[b][a] = c;
} floyd();
for (int i=;i<=R;i++)
for (int j=;j<=R;j++)
G[i][j] = mp[ goal[i] ][ goal[j] ]; memset(dp,0x3f,sizeof(dp));
for (int i=;i<=R;i++)
dp[i-][(<<(i-))] = ; for (int i=;i<(<<R);i++) //sta
for (int j=;j<=R;j++)
if ((<<(j-))&i) //qi
for (int k=;k<=R;k++) //zhong
dp[k-][i|(<<(k-))]=min(dp[k-][i|(<<(k-))],dp[j-][i]+G[j][k]); int ans = INF;
for (int i=;i<=R;i++)
ans=min(ans,dp[i-][(<<R)-]);
printf("%d\n",ans);
}

joisino's travel的更多相关文章

  1. AtCoder Beginner Contest 073

    D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...

  2. 图论 - Travel

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...

  3. 【BZOJ-1576】安全路径Travel Dijkstra + 并查集

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1044  Solved: 363[Sub ...

  4. Linux inode && Fast Directory Travel Method(undone)

    目录 . Linux inode简介 . Fast Directory Travel Method 1. Linux inode简介 0x1: 磁盘分割原理 字节 -> 扇区(sector)(每 ...

  5. HDU - Travel

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  6. 2015弱校联盟(1) - I. Travel

    I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...

  7. ural 1286. Starship Travel

    1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equ ...

  8. Travel Problem[SZU_K28]

    DescriptionAfter SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingC ...

  9. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

随机推荐

  1. ffmpeg 复用

    aa 将mkv中的音视频复用成ts流: ffmpeg -i 32_mkv_h264_718x480_ac3.mkv  -codec copy -bsf:v h264_mp4toannexb  -f m ...

  2. Selenium webdriver Java 查找元素

    1.简单查找 By ID: WebElement element=driver.findElement(By.id("userId")); By Name:WebElement e ...

  3. mac 上多版本python 共存

    Mac上自带了Python2.x的版本,有时需要使用Python3.x版本做开发,但不能删了Python2.x,可能引起系统不稳定,那么就需要安装多个版本的Python. 1.安装Python3.x版 ...

  4. 修改 Ubuntu 13.04 LAMP 服务器端口号

    因为今天想让一台Ubuntu 13.04服务器对外 web 服务的端口号为8000,自己改了一下,但是就是无法访问,端口后依然为 80.所以在网上找了一下修改端口的办法,原来我还少修改了一个文件,这里 ...

  5. C语言字符串操作总结大全(超具体)

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  6. 【Excle】二维表转化为一维表

    现在我们想做如下操作: 想把表1的数据转化为表2的数据,那么我们只需要如下这段代码即可解决需求 Sub 转置() Dim i%, arr arr = Application.InputBox(&quo ...

  7. blog url.txt

    java 回收机制与虚拟机http://www.cnblogs.com/zhanglei93/p/6636831.html Java 编程经验(牛人写的)  上/下http://www.thinksa ...

  8. mysql 应用场景

    一.按时间点来统计 ), date_FORMAT(date_Field,'%Y-%m-%d %H:00:00') as dateStr from table_name group by dateStr

  9. Docker经常使用命令

    Usage: docker [OPTIONS] COMMAND [arg...]  -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/ ...

  10. angularjs 可以加入html标签方法------ng-bind-html的用法总结(2)

    angular-ngSanitize模块-$sanitize服务详解 本篇主要讲解angular中的$sanitize这个服务.此服务依赖于ngSanitize模块. 要学习这个服务,先要了解另一个指 ...