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 ...
随机推荐
- Jacobi与SOR迭代法的实现与性能比较及均匀间距与Chebyshev插值的实现、性能分析及二者生成的插值误差比较
这篇文章给出(1)Jacobi与SOR迭代法的实现与性能比较及(2)均匀间距与Chebyshev插值的实现.性能分析及二者生成的插值误差比较,给出完整的实现代码,没有进行性能优化,仅供参考. (1)J ...
- CodeBlocks的安装配置以及使用教程
CodeBlocks的安装配置以及使用教程 教程写的很啰嗦,本来几句话就能搞定的,但为了照顾到那部分真正的小白还请大家见谅! 一.下载 前往CodeBlocks官网下载带编译器的版本,目前的最新版本为 ...
- 部分rpm包总结描述
acl-2.2.51-15.el7.x86_64 Commands for Manipulating POSIX(可移植操作系统接口 of unix) Access Control Lists.有ge ...
- slickgrid ( nsunleo-slickgrid ) 4 解决点击不切换单元格的问题
slickgrid ( nsunleo-slickgrid ) 4 解决点击不切换单元格的问题 上一次解决了列选择和区域选择冲突的问题,昨天太忙了,并且要陪小宝早点睡觉,就啥也没有赶上.今天上班面试. ...
- 订单退款&重复支付需求疑问点归纳整理
更新历史记录: 更新内容 更新人 更新时间 新建 Young 2020.12.10 16:45 更新产品疑问解答 Young 2020.12.11 10:14 更新退款权益终止时间 Young 2 ...
- Django之缓存、信号和图片验证码、ORM性能
一. 缓存 1. 介绍 缓存通俗来说:就是把数据先保存在某个地方,下次再读取的时候不用再去原位置读取,让访问速度更快. 缓存机制图解 2.Django中提供了6种缓存方式 1. 开发调试 2. 内存 ...
- 1.认识numpy的数组
认识ndarray --ndarray:用于存储单一数据类型的多维数组 ndarray基础属性的认识 import numpy as np #将numpy简写为np方便调用 1.通过以下实例来认识ar ...
- Fishing Master HDU - 6709
题目链接:https://vjudge.net/problem/HDU-6709 题意:给出n条鱼的抓捕时间k和煮鱼时间a[i],你要在最短时间把所有鱼都煮了.(注意你只有一口锅) 思路:首先我们必须 ...
- 获得PyInstaller打包exe的py源码
参考链接:https://laucyun.com/33359ed9f725529ac9b606d054c8459d.html way1:pyi-archive_viewer 提取pyc,uncomp ...
- KVM虚拟化配置
KVM虚拟化 虚拟化概念 KVM虚拟化概念详讲 虚拟化配置 首先开启虚拟化的支持 并且增加一个50g的硬盘 free查看内存 grep -Ei 'vmx|svm' /proc/cpuinfo查看虚拟机 ...