题目链接:

id=3411">点击打开链接

题目大意:有n个点。m条有向边,经过边须要一个花费,a b c p q代表 a到b的一条道路,假设经过这条边之前经过c点,那么须要p的花费,否则须要q的花费。问从1点到n点的最小花费。

方法1、每条边可能会经过多次,每一个点也能够经过多次,这样就没有了边界不能直接进行dfs,由于要记录之前经过的边。所以使用状压,dis[i][j]:当前在第i个点。j表示经过了的点,这样就能够得到推导的公式,按spfa的方式。跑出最短路

方法2、最多仅仅有10个点,那么假设1个点被反复经过3次以上,那么再经过它就仅仅会添加最小值,不会优化结果,也就是一个闸值,所以控制訪问点的次数。直接dfs+剪枝就能求解

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std ;
#define INF 0x3f3f3f3f
struct nn{
int a , b , c , p , q ;
int next ;
}edge[20];
int head[20] ,cnt ;
int n , m , min1 ;
struct node{
int a , k ;
int w ;
} node_p , node_q ;
queue <node> que ;
int vis[12][1100] , dis[12][1100] ;
void add(int a,int b) {
edge[cnt].a = a ; edge[cnt].b = b ;
scanf("%d %d %d", &edge[cnt].c, &edge[cnt].p, &edge[cnt].q) ;
edge[cnt].next = head[a] ; head[a] = cnt++ ;
}
int main() {
int i , a , b , c ;
while( scanf("%d %d", &n, &m) != EOF ) {
memset(head,-1,sizeof(head)) ;
memset(vis,0,sizeof(vis)) ;
memset(dis,INF,sizeof(dis)) ;
cnt = 0 ;
while( m-- ) {
scanf("%d %d", &a, &b) ;
add(a,b) ;
}
while( !que.empty() ) que.pop() ;
vis[1][1] = 1 ;
dis[1][1] = 0 ;
node_p.a = 1 ; node_p.k = 1 ;
node_p.w = 0 ;
que.push(node_p) ;
while( !que.empty() ) {
node_p = que.front() ;
que.pop() ;
//printf("bbiu--> %d %d\n", node_p.a, node_p.k) ;
vis[ node_p.a ][ node_p.k ] = 0 ;
for(i = head[ node_p.a ] ; i != -1 ; i = edge[i].next) {
b = edge[i].b ;
c = 1<<(edge[i].c-1) ;
if( node_p.k & c ){
if( dis[ node_p.a ][ node_p.k ] + edge[i].p < dis[ edge[i].b ][ node_p.k ] ) {
dis[ edge[i].b ][ node_p.k ] = dis[ node_p.a ][ node_p.k ] + edge[i].p ;
if( !vis[ edge[i].b ][ node_p.k ] ) {
vis[ edge[i].b ][ node_p.k ] = 1 ;
node_q.a = edge[i].b ;
node_q.k = node_p.k ;
node_q.w = dis[ edge[i].b ][ node_p.k ] ;
que.push(node_q) ;
}
}
}
else {
if( dis[ node_p.a ][ node_p.k ] + edge[i].q < dis[ edge[i].b ][ node_p.k|c ] ) {
dis[ edge[i].b ][ node_p.k|c ] = dis[ node_p.a ][ node_p.k ] + edge[i].q ;
if( !vis[ edge[i].b ][ node_p.k|c ] ) {
vis[ edge[i].b ][ node_p.k|c ] = 1 ;
node_q.a = edge[i].b ;
node_q.k = node_p.k|c ;
node_q.w = dis[ edge[i].b ][ node_p.k|c ] ;
que.push(node_q) ;
}
}
}
}
}
min1 = INF ;
for(i = 0 ; i < (1<<n) ; i++){
min1 = min(min1,dis[n][i]) ;
//printf("n - i == %d , %d \n", i, dis[n][i]) ;
}
if( min1 == INF )
printf("impossible\n") ;
else
printf("%d\n", min1) ;
}
return 0 ;
}

poj3411--Paid Roads(bfs+状压)的更多相关文章

  1. 孤岛营救问题 (BFS+状压)

    https://loj.ac/problem/6121 BFS + 状压 写过就好想,注意细节debug #include <bits/stdc++.h> #define read rea ...

  2. hdu 5094 Maze (BFS+状压)

    题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi ...

  3. hdu 4771 Stealing Harry Potter's Precious (BFS+状压)

    题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...

  4. 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP

    这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...

  5. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

  6. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  7. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  8. hdu 2209 bfs+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=2209 不知为啥有种直觉.会出状压+搜索的题,刷几道先 简单的BFS.状压表示牌的状态, //#pragma co ...

  9. HDU-4856 Tunnels (BFS+状压DP)

    Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...

随机推荐

  1. Codeforces 839 B. Game of the Rows-贪心

    最近太zz了,老是忘记带脑子... 补的以前的cf,发现脑子不好使...   B. Game of the Rows   time limit per test 1 second memory lim ...

  2. CF987A Infinity Gauntlet【STL】

    [链接]:CF987A [分析]:运用map [代码]: #include <iostream> #include<queue> #include<string.h> ...

  3. hadoop之深入浅出

    分布式文件系统与HDFS lHDFS体系结构与基本概念*** l数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管 ...

  4. python获取小程序手机号并绑定

    最近在做小程序开发,在其中也遇到了很多的坑,获取小程序的手机号并绑定就遇到了一个很傻的坑. 流程介绍 官方流程图 小程序使用方法 需要将 <button> 组件 open-type 的值设 ...

  5. elasticsearch 插件使用

    5.3.0新版本好像插件和开源的项目没有以前的多,官网就那么几个 常用的先安装Kibana: 提供炫丽的可视化图形展示并且作为elasticsearch的搜索的小清新客户端 1.下载安装包  wget ...

  6. Bean 生命周期&&模块化配置

    (一)审生命周期 1,配置一个方法作为生命初始化方法Spring会在对象创建后调用(init-method) 2,配置一个方法生命周期的销毁方法,spring容器在关闭并销毁所有容器中的对象之前调用. ...

  7. Word插入页码简单方法

    适用于只有一个首页,若是从某一页,需要分节符,从某一页参考Word2007插入两种页码. 插入-页码-页面低端-弹到设计窗口.然后选择页码-设置页码格式-起始页码0-勾选首页不同.

  8. ios 字体类型设置 倾斜加粗等

    后面加-Bold就是加粗 加-Oblique就是倾斜等 还有不懂请看http://iosfonts.com/ iPhone 5.0 iPad 5.0 Font Families: 58 Font Fa ...

  9. PowerPoint在线浏览的几个方案

    思路:将ppt转换成pdf.image后实现在线浏览功能 下面的解决方案均不用在服务器端安装office 一.找到一个收费的restful接口,测试可用 http://www.convertapi.c ...

  10. Http协议三次握手过程

    Http协议三次握手过程   TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...