题目链接:

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. HDU 1698.Just a Hook-线段树(成段替换、输出总和tree[1])

    HDU1698.Just a Hook 这个题是最最基础的成段更新的线段数的题目,直接贴代码吧. 代码: #include<iostream> #include<cstring> ...

  2. Longest Common Substring($LCS$)

    Longest Common Substring(\(LCS\)) 什么是子序列? 子序列就是某一个序列的不连续的一部分. 如图, \(abcde\)就是图中序列的一个子序列. 公共子序列 公共子序列 ...

  3. POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10378   Accepted: 2886 D ...

  4. Kth Smallest Element in a Sorted Matrix -- LeetCode

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  5. AFNetworking 2.0 Tutorial

    Update 1/18/2014: Fully updated for iOS 7 and AFNetworking 2.0 (original post by Scott Sherwood, upd ...

  6. Unable to list target platforms. Please make sure the android sdk path is correct. See the Console for more details.

    在android上发布遇到 androidSDK无法找到的问题 http://www.jianshu.com/p/fe4c334ee9fe

  7. http://jingyan.baidu.com/article/0eb457e5208cbb03f0a9054c.html

    http://jingyan.baidu.com/article/0eb457e5208cbb03f0a9054c.html

  8. iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【IOS】mac终端运行.sh文件总是提示permission denied

    如果我目录jni有一个list.sh文件 我直接 nxgametekiMacBook-Air:jni luonan$  ./list.sh ../../Classes 提示 permission de ...

  10. quick-cocos2d-x transition使用方法

    Functions transition.newEasing(action, easingName, more) 为图像创造效果 transition.execute(target, action,  ...