题目链接:

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. linq连接sqlite数据库(linq to sqlite) .net3.5

    http://www.cnblogs.com/xianyin05/archive/2012/12/23/2829905.html using Models; using System; using S ...

  2. ELK获取用户真实IP

    原理:在filebeat这台服务器上的nginx中获取到客户端真实IP($clientRealIp),    然后在访问日志中添加"$clientRealIp"字段.1. 通过ma ...

  3. Best Time to Buy and Sell Stock with Cooldown -- LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. apache mod_speling.so 忽略URL大小写(自动纠错)

    apache mod_speling.so 忽略URL大小写(自动纠错) 打开配置文件  httpd.conf 加入 LoadModule speling_module modules/mod_spe ...

  5. 前端JavaScript实现跨域的方式(转)

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  6. 【微信】微信小程序 调用this.setData报错this.setData is not a function;

    在调用方法过程中 报错如下: 代码如下: Page({ /** * 页面的初始数据 */ data: { userLocalInfo:'用户地址' }, /** * 返回swapping页面 */ b ...

  7. OpenGL投影矩阵【转】

    OpenGL投影矩阵 概述 透视投影 正交投影 概述 计算机显示器是一个2D平面.OpenGL渲染的3D场景必须以2D图像方式投影到计算机屏幕上.GL_PROJECTION矩阵用于该投影变换.首先,它 ...

  8. Elasticsearch 索引实例

    1.简述 ElasticSearch包含了一系列的感念,比如索引(indexing).搜索(search)以及聚合(aggregations),现在我们主要介绍indexing. 在Elasticse ...

  9. 【JUnit】Junit命令行执行、参数化执行、Main方法执行

    参考资料: main方法执行:http://stackoverflow.com/questions/2543912/how-do-i-run-junit-tests-from-inside-my-ja ...

  10. zabbix自定义监控项二

    为zabbix增加支持传参的自定义监控项 例如使用zabbix来监控tcp的12种状态 tcp的12种状态可以通过man netstat来找到,即 LISTEN:等待从任何远端TCP 和端口的连接请求 ...