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的钱.求的是最少要花 ...
随机推荐
- 选择器的使用(nth-child和nth-last-child选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- NTKO在线office控件使用实例
目录 1. NTKO在线office控件使用实例 1.1. 基础介绍 1.2. 基本原理 1.3. 实例 1.3.1. 打开.保存部分代码 1.3.2. 动态设值 1. NTKO在线office控件使 ...
- JButton点击事件
JButton点击事件: 以前都是搞一个JFrame,放个JButton,然后用鼠标点击: 忽然之间: import javax.swing.JButton; public class Page06 ...
- 自已实现一个UI库
[2014年写一个UI库时写的几个文章,公布出来] 几年前的一个嵌入式的UI开发,使自己有机会接触到了UI的一些底层知识,尽管之前也开发过非常多Windows下的信息应用系统,也做非常多的界面开发,但 ...
- Android自己主动提示文本框(AutoCompleteTextView)
自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...
- C语言之基本算法25—牛顿迭代法求方程近似根
//牛顿迭代法! /* ============================================================ 题目:用牛顿迭代法求解3*x*x*x-2*x*x-16 ...
- MySQL-数据表锁定
MySQL允许客户端会话明确获取表锁,以防止其他会话在特定时间段内访问表.客户端会话只能为自己获取或释放表锁.它不能获取或释放其他会话的表锁. 创建一个数据表: CREATE DATABASE IF ...
- ARM+llinux系统移植3G拨号上网收发短信(一)【转】
本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17099737 一. PPP移植 各项工作具体说明 向Linux内核添加3 ...
- bzoj3612 [Heoi2014]平衡——整数划分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3612 看了好久才弄清楚题意... 原来整数划分就是这样的啊:https://blog.csd ...
- TI BLE:读本机地址
uint8 ownAddress[B_ADDR_LEN]; //B_ADDR_LEN=6GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress); #def ...