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. 基于php的银行卡实名认证接口调用代码实例

    银行卡二元素检测,检测输入的姓名.银行卡号是否一致. 银行卡实名认证接口:https://www.juhe.cn/docs/api/id/188 <?php // +-------------- ...

  2. 利用问答机器人API开发制作聊天类App

    缘起 很久没写项目了,所以单纯的想练练手,正好看到有问答机器人的接口,想到之前也做过聊天项目,为什么不实验一下呢.当然也是简单调用接口的项目,并没有真正的完成问答的算法等等.业余项目,功能不齐全,只实 ...

  3. left与margin-left区别

    left,right,top,bottom仅对于position:relative|absolute|fixed的元素有意义. <!DOCTYPE html PUBLIC "-//W3 ...

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

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

  5. PHP抓取网络数据

    涉及到的知识点不多 file_get_contents:读取数据: preg_match_all:正则匹配: 和匹配之后的数据分析. 不同网页所需要抓取的数据是不同的,所以正则表达式自然也不一样,针对 ...

  6. strrev 字符串反转函数

    strrev (PHP 3, PHP 4, PHP 5) strrev -- Reverse a string Description string strrev ( string string ) ...

  7. implode 把数组 组成一个字符串

    $data=array(1,2,3,4,5); implode(",",$data);

  8. Web应用程序使用Hibernate

    在本文中,我们将学习使用hibernate创建一个Web应用程序. 对于创建Web应用程序,我们使用JSP表示逻辑层,使用Bean类表示数据,以及使用DAO类操作数据库.在hibernate中创建简单 ...

  9. xilinx 赛灵思fpga verilog hdl 教程

    http://www.eefocus.com/article/08-03/37231s.html http://wenku.baidu.com/link?url=5mdkMmm4BGGi7gRdgSk ...

  10. 音频处理之去噪算法---基于pcm和g711的音频16000hz、8bit去噪声算法

    (1)应用背景 (2)主要降噪算法原理 (3)算法流程 (4)算法实现 (5) ------------author:pkf -------------------time:2-6 --------- ...