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≤M≤N×(N−1)⁄2
- 2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)
- ri≠rj(i≠j)
- 1≤Ai,Bi≤N,Ai≠Bi
- (Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(i≠j)
- 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
3 3 3
1 2 3
1 2 1
2 3 1
3 1 4
Sample Output 1
2
For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.
Sample Input 2
3 3 2
1 3
2 3 2
1 3 6
1 2 2
Sample Output 2
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
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的更多相关文章
- AtCoder Beginner Contest 073
D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- 【BZOJ-1576】安全路径Travel Dijkstra + 并查集
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1044 Solved: 363[Sub ...
- Linux inode && Fast Directory Travel Method(undone)
目录 . Linux inode简介 . Fast Directory Travel Method 1. Linux inode简介 0x1: 磁盘分割原理 字节 -> 扇区(sector)(每 ...
- HDU - Travel
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- ural 1286. Starship Travel
1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equ ...
- Travel Problem[SZU_K28]
DescriptionAfter SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingC ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
随机推荐
- ios app在itunesConnect里面的几种状态
原地址:http://blog.csdn.net/dean19900504/article/details/8164734 Waiting for Upload (Yellow) Appears wh ...
- iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐
代码地址如下:http://www.demodashi.com/demo/13208.html 前言 我们首先要在AppDelegate里面进行iOS的适配,可以参考这篇文章 iOS原生推送(APNS ...
- lucene 索引中文档的属性建立与不建立带来的影响总结
索引中文档的属性建立与不建立带来的影响总结 1.依据文档的某属性去查找索引的话,只会返回带有此属性(如果你对当前属性设定了条件,那么需要满足当前条件)的所有文档,没有建立此属性的文档是不会在返回结 ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- Titel Block不显示
在原有原理图上New page时不显示Titel Block 解决办法:两种(1)(2) 方法1.在新建的原理图上右键选择Schematic Page Properties,按下图勾选即可 方法2.进 ...
- unity, 烘焙lightmap
1,建一个名为_scene的场景,放一个球体. 2,将球体的Static属性勾选. 3,将默认光源Directional light的Baking属性改为Baked. 4,打开Window->L ...
- js使用正则表达式验证身份证格式
function checkIdentity(identity){ var reg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/; i ...
- iOS开发密码输入数字和字母混合
#import "TestInPut.h" @implementation TestInPut +(BOOL)judgePassWordLegal:(NSString *)pass ...
- App打包上架流程(iOS转)
由于苹果的机制,在非越狱机器上安装应用必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节. AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传A ...
- Json介绍以及解析Json
首先,介绍一下Json字串,以下Json的介绍引用网上资料. 简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应 ...