poj 3411 Paid Roads
题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用。
DFS
刚开始想到了用搜索,自己搜索学的不好,有几个问题解决不了,首先就是每个点可以走多次,不能用0,1标记一个点是否走过,如果不标记就可能在一直走一条循环路,还有就是不会回溯,总之,自己就是不会写...
网上抄的别人的代码,每个点最多可以走3次,回溯也挺简单...
#include<stdio.h>
#include<string.h> int n,m;
struct node{
int a,b,c,p,r;
};
node road[];
int visit[],mincost;
void DFS(int a,int fee)
{
if(a==n && mincost>fee)
{
mincost=fee;
return;
}
for(int i=;i<=m;i++)
{
if(a==road[i].a && visit[road[i].b]<=)
{
int b=road[i].b;
visit[b]++;
if(visit[road[i].c])
DFS(b,fee+road[i].p);
else
DFS(b,fee+road[i].r);
visit[b]--;
}
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(visit,,sizeof(visit));
visit[]=;
mincost=;
for(int i=;i<=m;i++)
scanf("%d%d%d%d%d",&road[i].a,&road[i].b,&road[i].c,&road[i].p,&road[i].r);
DFS(,);
if(mincost==)
printf("impossible\n");
else
printf("%d\n",mincost);
}
return ;
}
poj 3411 Paid Roads的更多相关文章
- 多次访问节点的DFS POJ 3411 Paid Roads
POJ 3411 Paid Roads Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 24 ...
- poj 3411 Paid Roads(dfs)
Description A network of m roads connects N cities (numbered to N). There may be more than one road ...
- poj 3411 Paid Roads很水的DFS
题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...
- POJ 3411 Paid Roads(DFS)
题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- 【题解】Paid Roads [SP3953] [Poj3411]
[题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...
随机推荐
- C++中把string转成char
c_str函数的返回值是const char*的,不能直接赋值给char*, c++语言提供了两种字符串实现,其中较原始的一种只是字符串的c语言实现. 与C语言的其他部分一样,它在c+的所有实现中可用 ...
- Android通过PHP连接MySQL(用到Json)
1下载phpnow 如果已经有mysql 则需要换一个端口 在服务器机器上的phpnow安装目录E:\PHPnow-1.5.5\htdocs下新建一个test.php文件: 其中我用的数据库是test ...
- 【JSONKit】序列化Dictionary崩溃
jsonkit通过Dictionary转换成JSON字符串时总是崩溃. 解析代码: 崩溃地点 分析是因为我的参数中全是数字 找了一下原因,不知道知道怎么设置,(求大神指点) 这里有一个折中办法使用 ...
- php Debugging with Xdebug and Sublime Text 3(转)
Debugging – we all do it a lot. Writing code perfectly the first time around is hard and only a few ...
- UI事件之load
load事件属于CSS3规范中的UI事件,load事件处理程序在页面元素和资源(html/script/link/img等)全部加载完成后在window上触发,或在img元素加载完成后再img元素上触 ...
- 已知要闪回的大致时间使用基于as of scn的闪回查询
基本判断出要恢复误操作的dml的时间可以使用如下的方法进行数据的恢复: example: 一.创建test表 -------create table flashback_asof------ crea ...
- PowerDesigner15在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
转载: http://blog.csdn.net/successful555/article/details/7582154 PowerDesigner中如何设置字符编码为GBK或者GB2312 ht ...
- html5本地存储 local storage
HTML5 web storage, a better local storage than cookies. With HTML5, web pages can store data locally ...
- sa命令从/var/account/pacct原始记账数据文件读取信息并汇总
sa命令从/var/account/pacct原始记账数据文件读取信息并汇总
- 13个JavaScript图表(JS图表)图形绘制插件
转自:http://blog.jobbole.com/13671/ 1. Flash 过去是最佳解决方案,但很多人多在从那迁移: 2. 现代浏览器及其更强大的计算能力,使其在转化绘制实时数据方面的能力 ...