题意:有n座城市和m(1<=n,m<=10)条路。现在要从城市1到城市n。有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱,

如果没有去过就付R的钱。求的是最少要花多少钱。

析:BFS,然后由于走的路线不同,甚至边或者点都可能多走,所以用状态压缩。然后本题有坑啊,有重连,而且有很多条重边,所以多走几次就好了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 100000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
int to, c, p, r, next;
Node(){ }
Node(int t, int cc, int pp, int rr, int n)
: to(t), c(cc), p(pp), r(rr), next(n) { }
};
Node a[maxn*2];
int head[maxn], cnt; void add_edge(int u, int v, int c, int p, int r){
a[cnt] = Node(v, c, p, r, head[u]);
head[u] = cnt++;
} struct State{
int state, pos;
State(int s, int p) : state(s), pos(p) { }
};
int dp[1<<10][10];
int vis[10]; int bfs(){
queue<State> q;
int ans = INF;
q.push(State(0, 0));
memset(dp, INF, sizeof dp);
memset(vis, 0, sizeof vis);
dp[0][0] = 0; while(!q.empty()){
State u = q.front(); q.pop();
int state = u.state;
if(u.pos + 1 == n){
ans = min(ans, dp[state][u.pos]);
continue;
}
for(int i = head[u.pos]; ~i; i = a[i].next){
int v = a[i].to;
int c = a[i].c;
int r = a[i].r;
int p = a[i].p;
if(vis[v] > 50) continue;
++vis[v];
dp[state|(1<<v)][v] = min(dp[state|(1<<v)][v], dp[state][u.pos] + ((state&(1<<c)) ? p : r));
q.push(State(state|(1<<v), v));
}
}
return ans;
} int main(){
while(scanf("%d %d", &n, &m) == 2){
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 0; i < m; ++i){
int u, v, c, p, r;
scanf("%d %d %d %d %d", &u, &v, &c, &p, &r);
add_edge(u-1, v-1, c-1, p, r);
}
int ans = bfs();
if(ans == INF) printf("impossible\n");
else printf("%d\n", ans);
}
return 0;
}

  

POJ 3411 Paid Roads (状态压缩+BFS)的更多相关文章

  1. 多次访问节点的DFS POJ 3411 Paid Roads

    POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 24 ...

  2. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  3. POJ 3411 Paid Roads(SPFA || DFS)

    题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...

  4. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  5. poj 3411 Paid Roads(dfs)

    Description A network of m roads connects N cities (numbered to N). There may be more than one road ...

  6. poj 3411 Paid Roads很水的DFS

    题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...

  7. POJ 3411 Paid Roads(DFS)

    题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...

  8. poj 3411 Paid Roads

    题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用. ...

  9. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. mac下cocos+android在.bash_profile文件里的配置

    既包含了已经消失了的老板本"cocos"软件相关的配置,也包含当时最新的cocos2d-x-3.11引擎包的相关配置 支持把cocos引擎相关代码预编译出库文件存放到prebuil ...

  2. Storm的并行度、Grouping策略以及消息可靠处理机制简介

    转自:https://my.oschina.net/zc741520/blog/409949 概念: Workers (JVMs): 在一个节点上可以运行一个或多个独立的JVM 进程.一个Topolo ...

  3. C#异步编程(四)混合模式线程同步

    之前讨论了基元用户模式和内核模式线程同步构造.其他所有线程同步构造都基于它们,而且一般都合并了用户模式和内核模式构造,我们称为混合线程同步构造.没有线程竞争时,混合构造提供了基元用户模式构造所具有的性 ...

  4. 使用Jsonp实现跨域请求

    来自百度百科的一段话: JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.由于同源策略,一般来说位于 server1.exampl ...

  5. mysql之 共享表空间与独立表空间、frm,MYD,MYI.idb,par文件说明

    一.共享表空间与独立表空间MySQL5.5默认是共享表空间 ,5.6中,默认是独立表空间. 共享表空间:ibdata1是InnoDB的共享表空间,默认配置是把全部表空间存放到ibdata1中,因此而造 ...

  6. CEF源码编译

    CEF的构造说明:https://bitbucket.org/chromiumembedded/cef/wiki/BranchesAndBuilding chromium的源码地址:https://c ...

  7. [转载]Sleep(x)的使用和sleep(0)作用解析

    链接:http://blog.csdn.net/m_leonwang/article/details/28434383 假设现在是 2012-12-16 3:37:40,如果我调用一下 Thread. ...

  8. Eclipse中的普通Java项目如何部署到Tomcat中

    我现在的做法: 1.在Eclipse中配置Tomcat时,选择创建Context文件,而不是server.xml,好处是文件可以随便命名,与虚拟目录一致(即xml中的属性path).Tomcat加载项 ...

  9. java中如何将OutputStream转换为InputStream

    在不需要文件生成的情况下,直接将输出流转换成输入流.可使用下面的三种方法: 如果你曾经使用java IO编程,你会很快碰到这种情况,某个类在OutputStream上创建数据而你需要将它发送给某个需要 ...

  10. js查看对象内容

    function show_obj(obj){ var temp,p1Str=""; for(temp in obj){ p1Str=p1Str+temp+":" ...