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

直接DFS搜。

链接http://poj.org/problem?id=3411

代码:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <stack>
#define loop(s,i,n) for(i = s;i < n;i++)
#define cl(a,b) memset(a,b,sizeof(a))
const int maxn = ;
const int inf = ;
using namespace std;
struct node
{
int u,v,c,r,p;
};
vector <struct node >g[];
int vis[];
int ans;
int n,m;
void dfs(int u,int val)
{
vis[u]++;
if(u == n)
{
if(ans > val)
ans = val;
return ;
} if(ans < val)
return; int i; for(i = ;i < g[u].size();i++)
{
int v;
v = g[u][i].v;
int c;
c = g[u][i].c;
if(vis[v] <= )
{
int min;
min = inf;
if(vis[c] && min > g[u][i].p)
min = g[u][i].p;
if(min > g[u][i].r)
min = g[u][i].r; dfs(v,val+min);
vis[v]--;
}
} } int main()
{
int u,v,t,p,r;
while(~scanf("%d %d",&n,&m))
{
int i,j;
memset(vis,,sizeof(vis));
for(i = ;i <= n;i++)
g[i].clear(); while(m--)
{
scanf("%d%d%d%d%d",&u,&v,&t,&p,&r);
g[u].push_back((struct node){u,v,t,r,p});
}
ans = inf;
dfs(,);
if(ans != inf)
cout<<ans<<endl;
else
puts("impossible");
}
return ;
}

poj 3411 Paid Roads很水的DFS的更多相关文章

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

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

  2. poj 3411 Paid Roads(dfs)

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

  3. POJ 3411 Paid Roads(DFS)

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

  4. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

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

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

  6. poj 3411 Paid Roads

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

  7. POJ 3411 Paid Roads (状态压缩+BFS)

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

  8. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  9. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

随机推荐

  1. 自动填写IE的网页的输入框的内容

    procedure TForm1.PutData; var ShellWindow: IShellWindows; nCount: integer; spDisp: IDispatch; i,j,X: ...

  2. vmware12 安装linux centos6

          内核选 2.6                                                                 中文安装选 基本系统 -> 基本, 兼 ...

  3. The 15th UESTC Programming Contest Preliminary D - Destr0y City cdoj1558

    地址:http://acm.uestc.edu.cn/#/problem/show/1558 题目: D - Destr0y City Time Limit: 3000/1000MS (Java/Ot ...

  4. Linux树莓派中/etc/rc.local不执行的问题

    最近研究在树莓派中嵌入式开发java程序,并打算和Salesforce进行通信.需要开发一个java的web server,不想弄那么复杂,于是打算在linux系统中/etc/rc.local写想要执 ...

  5. WPS宏不可用解决方法

    在使用WPS Office过程中,遇见宏不可用,在启用宏的过程中提示获取VBA插件 解决方法: 1.下载VBA插件,下载地址:https://pan.baidu.com/s/1LqBmXw37U0km ...

  6. Web Servlet的体系架构

    Servlet为根接口,里面有5个方法,init() servlet初始化,将ServletConfig作为参数传入,service() 响应请求,destroy() 销毁servlet,getSer ...

  7. c++ 跳转语句块

    p170~p172:跳转语句:1.break:对while for switcho有效!2.continue:中断当前迭代,但是循环还要继续.因此对while for有效,对switch无效!3.go ...

  8. Cisco 交换Trunk配置

    交换Trunk配置 #进入直连端口下 config)#int f0/1 #配置trunk封装协议 config)#switchport trunk encapsulation dot1q #配置为tr ...

  9. chrome中,BackstopJS 使用cookie文件后依旧提示需要登录

    1. 本地环境: apache-tomcat:7.05,jdk8, backstopJS:latest-version 配置tomcat管理用户成功:手工访问tomcat应用内部服务正常: 2.  问 ...

  10. Can't connect to any repository: xxxxxx Error writing request body to server

    今天在git提交代码时一直报如下错误: Can't connect to any repository: https://gitee.com/xxxxxx(https://gitee.com/xxxx ...