hdu3768 spfa+全排列
题意:
给你一个无向图,和一些必须经过的点,问你从起点出发,到达所有必须经过的点再回来的最小总路径.
思路:
因为必须经过的点的数量很小,小于等于10,全排列是 10! = 3628800 所以以每个必须经过的点为起点跑最短路,记录数值,然后全排列,枚举经过顺序,取得最小就行了..
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm> #define N_node (100000 + 500)
#define N_edge (200000 + 1000)
#define INF 1000000000
using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; STAR E[N_edge];
int list[N_node] ,tot;
int s_x[N_node];
int s_x2[12][N_node];
int mk_node[12];
int hash[N_node]; void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
} void SPFA(int s ,int n)
{ int mark[N_node] = {0};
for(int i = 0 ;i <= n ;i ++)
s_x[i] = INF;
mark[s] = 1;
s_x[s] = 0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int xin ,tou;
tou = q.front();
q.pop();
mark[tou] = 0;
for(int k = list[tou] ;k ;k = E[k].next)
{
xin = E[k].to;
if(s_x[xin] > s_x[tou] + E[k].cost)
{
s_x[xin] = s_x[tou] + E[k].cost;
if(!mark[xin])
{
mark[xin] = 1;
q.push(xin);
}
}
}
}
return ;
} int main ()
{
int t ,n ,m ,i;
int a ,b ,c ,s;
scanf("%d" ,&t);
while(t--)
{
scanf("%d %d" ,&n ,&m);
memset(list ,0 ,sizeof(list));
tot = 1;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d" ,&a ,&b ,&c);
a++ ,b++;
add(a ,b ,c) ,add(b ,a ,c);
}
scanf("%d" ,&s);
for(i = 1 ;i <= s ;i ++)
{
scanf("%d" ,&mk_node[i]);
mk_node[i]++;
hash[mk_node[i]] = i;
}
mk_node[0] = 1;
for(i = 0 ;i <= s ;i ++)
{
SPFA(mk_node[i] ,n);
for(int j = 1 ;j <= n ;j ++)
s_x2[i][j] = s_x[j];
} int max = 1;
for(i = 1 ;i <= s ;i ++)
max *= i;
int ans_min = INF;
while(max --)
{
int tmp = s_x2[0][mk_node[1]] + s_x2[hash[mk_node[s]]][1];
for(i = 2 ;i <= s ;i ++)
tmp += s_x2[hash[mk_node[i-1]]][mk_node[i]];
if(ans_min > tmp) ans_min = tmp;
next_permutation(mk_node + 1 ,mk_node + s + 1);
}
printf("%d\n" ,ans_min);
}
return 0;
}
hdu3768 spfa+全排列的更多相关文章
- Shopping(SPFA+DFS HDU3768)
Shopping Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- PHP实现全排列(递归算法)
算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为: ① 如果n=1,则排列P只有一 ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
随机推荐
- AntDesign Pro + .NET Core 实现基于JWT的登录认证
很多同学说AgileConfig的UI实在是太丑了.我想想也是的,本来这个项目是我自己使用的,一开始甚至连UI都没有,全靠手动在数据库里修改数据.后来加上了UI也是使用了老掉牙的bootstrap3做 ...
- HTML5基础入门一天学完
HTML 什么是HTML HTML:Hyper Text Markup Language(超文本编辑语言) HTML的发展史 HTML5优势 世界知名浏览器厂商对HTML5的支持 市场的需求 跨平台 ...
- golang——win10环境protobuf的使用
1.protobuf配置 (1)https://github.com/protocolbuffers/protobuf/releases (2)选择适合的版本:protoc-3.8.0-win64.z ...
- Docker 三剑客 到 k8s 介绍
一.Docker 三剑客 Docker-Compose Docker-Compose 是用来管理你的容器的,有点像一个容器的管家,想象一下当你的Docker中有成百上千的容器需要启动,如果一个一个的启 ...
- docker配置私有镜像仓库-registry和hyper/docker-registry-web
1.前言️ Docker hub是远程仓库,是国外的,push pull速度特别慢,尤其是网速不好的时候,页面都点不进去,官网 但是可以配置阿里云镜像加速哦: 因此搭建一个私有的镜像仓库用于管理我们 ...
- react+ts封装AntdUI的日期选择框之月份选择器DatePicker.month
需求:由于在项目开发中,当需要使用该组件时都需要对该组件进行大量的代码输出,为了方便代码统一管理,减少冗余代码,所以将此组件进行二次封装. 其他成员在使用中只需将自己的设置通过对应的参数传递到该组件, ...
- beego框架panic: 'GetSecurityInf' method doesn't exist in the controller CorporateInfcontroller问题解决
在使用beego框架时,出现类似于panic: 'GetSecurityInf' method doesn't exist in the controller CorporateInfcontroll ...
- 自然语言处理(NLP)知识结构总结
自然语言处理知识太庞大了,网上也都是一些零零散散的知识,比如单独讲某些模型,也没有来龙去脉,学习起来较为困难,于是我自己总结了一份知识体系结构,不足之处,欢迎指正.内容来源主要参考黄志洪老师的自然语言 ...
- upx 手动脱壳
查壳 UPX 0.89.6 - 1.02 / 1.05 - 2.90 (Delphi) stub -> Markus & Laszlo upx这类压缩壳手动脱壳非常简单. 一.查找oep ...
- Django 模板 render传参不转码
今天通过Django后端向前端页面传递一行js代码,却发现符号被转码了导致代码不能执行 Django代码 HTML代码 实际生成页面代码 我们可以看到实际代码中的引号被转义,导致代码不能执行, 解决方 ...