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 ...
随机推荐
- rails delegate机制
Delegate是一种应用composite来代替extend的机制,可以有效地降低代码的耦合性. Rails 2.2增加了delegate方法,可以十分方便地实现delegate机制. 01.def ...
- 在对方电脑建立IPC连接, 利用IPC$入侵 运行木马
第一大步: IPC漏洞的建立 1)在目标主机上设置组策略:開始->执行-〉gpedit.msc 2)计算机配置->windows配置-〉本地策略-〉安全选项 3)在安全选项中, 将网络訪 ...
- win8 推送通知 小记
http://blog.csdn.net/nacl025/article/details/8998552 http://blog.csdn.net/nacl025/article/details/90 ...
- 基于php的银行卡实名认证接口调用代码实例
银行卡二元素检测,检测输入的姓名.银行卡号是否一致. 银行卡实名认证接口:https://www.juhe.cn/docs/api/id/188 <?php // +-------------- ...
- Jmeter-性能测试(三)
一.调通脚本(以json串Post接口为例)添加聚合报告(线程组->添加->监听器->聚合报告)并调试好需要压测的脚本,如下已经调通的P_C_B151就是我需要压测的脚本 二.设置场 ...
- SVN环境搭建(1)
原文地址:http://www.penglig.com/post-72.html Subversion 是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建 SVN 服务 ...
- POJ-3134-Power Calculus(迭代加深DFS)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
- stm32 IDR寄存器软件仿真的BUG
/* * 函数名:Key_GPIO_Config * 描述 :配置按键用到的I/O口 * 输入 :无 * 输出 :无 */ void Key_GPIO_Config(void) { GPIO_Init ...
- TensorFlow学习笔记 速记2 报错:failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_INVALID_DEVICE
版本: tensorflow-gpu 原因: 在创建session时没有使用我想让它用的gpu 解决方案: 1. 在python程序中: import os os.environ["CUDA ...
- Android WebView 常见问题
1.为WebView自定义错误显示界面: /** * 显示自定义错误提示页面,用一个View覆盖在WebView */ protected void showErrorPage() { LinearL ...