poj3411 Paid Roads
思路:
搜索。注意点和边都有可能经过多次。
实现:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = , INF = 0x3f3f3f3f;
int n, m;
struct edge
{
int to, c, p, r;
};
vector<edge> G[MAXN];
int vis[MAXN]; int dfs(int now)
{
if (now == n)
return ;
int ans = INF;
for (int i = ; i < G[now].size(); i++)
{
if (vis[G[now][i].to] > ) continue;
vis[G[now][i].to]++;
int x = dfs(G[now][i].to) + G[now][i].r;
ans = min(ans, x);
int y = INF;
if (vis[G[now][i].c])
y = dfs(G[now][i].to) + G[now][i].p;
ans = min(ans, y);
vis[G[now][i].to]--;
}
return ans;
} int main()
{
cin >> n >> m;
int a, b, c, p, r;
for (int i = ; i < m; i++)
{
cin >> a >> b >> c >> p >> r;
G[a].push_back(edge{b, c, p, r});
}
vis[] = true;
int ans = dfs();
if (ans == INF) puts("impossible");
else cout << ans << endl;
return ;
}
poj3411 Paid Roads的更多相关文章
- 【题解】Paid Roads [SP3953] [Poj3411]
[题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...
- 多次访问节点的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 ...
- Paid Roads POJ - 3411
A network of m roads connects N cities (numbered from 1 to N). There may be more than one road conne ...
- 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
题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用. ...
- poj 3411 Paid Roads很水的DFS
题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
随机推荐
- Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
Jedis是Redis的Java客户端,Spring将Jedis连接池作为一个Bean来配置.如果在Spring Data的官网上可以发现,Spring Data Redis已经将Jedis集成进去了 ...
- 爱普生L201
http://tech.sina.com.cn/b/2011-03-29/05481698131.shtml
- ATM取款机模拟——数据结构课设
今天帮人写的第二篇课设 . ;-) 机智的窝 要求:大概说一下吧,就是要创建一个用户(初始化一账户),模拟ATM的业务(取款,100的整数倍,改密 码,查剩余金额.等等,各 ...
- vmstat输出项解释
输出项的解释例如以下: procs * r列表示执行和等待cpu时间片段的进程数,这个值假设长期大约系统cpu个数.说明cpu不足 * b列表示在等待资源的进程数.比方正在等待IO或者内存交换等等 m ...
- Android系统之路(初识MTK) ------ OTA打包ROM安装系统img等到ZIP
在做OTA升级包的时候,我编译了好多次都没过.老是IO异常.刚開始以为是我 make 的错误.后来多次检查 Error 发现是我的配置信息写错了,与驱动project师一起检查源代码, 改动配置信息后 ...
- iOS学习笔记(4) — UITableView的 重用机制
iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...
- git 在一台机器上配置多个账户
前提: 必须知道怎样配置git账户,请參考git官方教程:https://help.github.com/articles/generating-ssh-keys 这个教程能教你怎样生成ssh-key ...
- (七)Java 变量类型
Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ...
- iOS开发——高级篇——iOS 强制退出程序APP代码
1.先po代码 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@" ...
- ACdream 1412 DP+排列组合
2-3 Trees Problem Description 2-3 tree is an elegant data structure invented by John Hopcroft. It is ...