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 ...
随机推荐
- Android下的数据存储与訪问 --- 以文件的形式
Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...
- 微信小程序 - 表单验证插件WxValidate(自定义警告信息形式)
弹出的形式对于用户来说,总是不太友好的 可能会出现层级问题(只需要设置一下提示的层级即可) WxValidate内置规则 以下代码拷贝即可使用~ wxml <form bindsubmit='s ...
- POJO百度百科
POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. 使用POJO名称是为了避免和EJB混淆起来, ...
- <转> lua: userdata的metatable使用
1 如何封装c++的指针 对于c++对象的lua包装,我们可以使用 template<typename T> struct luaUserdataWrapper { luaUserdat ...
- ibatis常用的集中判断语句
http://blog.csdn.net/liaomin416100569/article/details/5344483
- springboot学习(六) springboot开发web应用
1.简介 Spring Boot非常适合开发web应用程序.你可以使用内嵌的Tomcat,Jetty或Undertow轻轻松松地创建一个HTTP服务器.大多数的web应用都使用spring-boot- ...
- W25Q128页数和扇区数
int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size){ *block_size = 4 ...
- 妙味云课堂之css:其它知识点汇总
一. 热区 map 热区.area 点击区域 shape="circle" 圆型,coords="圆心点X.圆心点Y,圆的半径" shape="rec ...
- 贯通tomcat --- 电子书
http://www.educity.cn/jiaocheng/j10865.html 第1章 认识Tomcat [本章导读] Tomcat服务器是一个免费的开放源代码的Web应用服务器.它是Apac ...
- Shift Register(Using Submodule)
/*************************************************** / Shift Register module by Submodule / Progra ...