POJ 3411 Paid Roads(SPFA || DFS)
题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长。
思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态压缩一下,第二维来记录状态,表示到过这个点的第几个状态。也可以用DFS,因为最多十个点,所以如果走某一个点走过三遍说明就是真的只增加费用了,也就是真正的在走环路了。DFS分析
二维SPFA
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream> using namespace std ; struct node
{
int uu ;
int p,r ;
bool vis ;
} mapp[][]; int N,m ;
int dis[][] ;
int cost ;
bool vist[] ;
const int INF = ; int spfa()
{
queue<pair<int,int> >Q ;
Q.push(make_pair(,)) ;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
dis[i][j] = INF ;
memset(vist,false ,sizeof(vist)) ;
dis[][] = ;
while(!Q.empty())
{
int a = Q.front().first ;
int b = Q.front().second ;
Q.pop() ;
vist[a] = false ;
for(int i = ; i <= N ; i++)
{
if(mapp[a][i].vis)
{
if( b & ( << (mapp[a][i].uu-))) cost = mapp[a][i].p ;//減一是為了讓狀態從1開始,1,2,4....
else cost = mapp[a][i].r ;
if(dis[i][b | ( << (mapp[a][i].uu-))] > cost + dis[a][b])
{
dis[i][b | ( << (mapp[a][i].uu-))] = cost + dis[a][b] ;
if(!vist[i])
{
vist[i] = true ;
Q.push(make_pair(i,b | ( << (mapp[a][i].uu-)))) ;
}
}
}
}
}
int minn = INF ;
for(int i = ; i < << N ; i++)
minn = min(minn,dis[N][i]) ;
return minn ;
}
int main()
{
int u,v,uu,p,r ;
while(~scanf("%d %d",&N,&m))
{
for(int i = ; i <= m ; i++)
{
scanf("%d %d %d %d %d",&u,&v,&uu,&p,&r) ;
mapp[u][v].vis = true ;
mapp[u][v].uu = uu ;
mapp[u][v].p = p ;
mapp[u][v].r = r ;
}
int ans = spfa() ;
if(ans < INF)
printf("%d\n",ans) ;
else printf("impossible\n") ;
}
return ;
}
DFS
#include <stdio.h>
#include <string.h>
#include <iostream>
#define oo 9999999
using namespace std ; int N,M ;
struct node
{
int u,v,w ;
int P,R ;
} road[];
int vis[] ,ans; void dfs(int u,int cost)
{
if(u == N && ans > cost)
{
ans = cost ;
return ;
}
for(int i = ; i < M ; i++)
{
if(u == road[i].u && vis[road[i].v] <= )
{
vis[road[i].v] ++ ;
if(vis[road[i].w])
dfs(road[i].v,cost+road[i].P) ;
else dfs(road[i].v,cost+road[i].R) ;
vis[road[i].v] -- ;
}
}
}
int main()
{
while(~scanf("%d %d",&N,&M))
{
for(int i = ; i < M ; i++)
scanf("%d %d %d %d %d",&road[i].u,&road[i].v,&road[i].w,&road[i].P,&road[i].R) ;
memset(vis,,sizeof(vis)) ;
vis[] = ;
ans = oo ;
dfs(,) ;
if(ans == oo)
printf("impossible\n") ;
else
printf("%d\n",ans) ;
}
return ;
}
POJ 3411 Paid Roads(SPFA || DFS)的更多相关文章
- poj 3411 Paid Roads(dfs)
Description A network of m roads connects N cities (numbered to N). There may be more than one road ...
- 多次访问节点的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
题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少. 直接DFS搜. 链接http://poj.org ...
- POJ 3411 Paid Roads(DFS)
题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...
- poj 3411 Paid Roads
题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用. ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
- 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{[ ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
随机推荐
- AndroidStudio中gradle异常:unexpected end of block data
原因:可能是Android buildTools版本不够高. 解决方法:打开build.gradle,将android中buildToolsVersion改为'20.0.0' (我使用的是gradle ...
- OpenDaylight之openflowjava的编译
最近因为工作需要开始研究opendaylight,首先需要的是编译项目. 因为项目代码都是在git.opendaylight.org上的,所以需要先安装git工具. 另因为opendaylight项目 ...
- 【转载】Powershell设置世纪互联Office365嵌套组发送权限
Start-Transcript ".\Set-GroupSendPermisionLog.txt" -Force function Get-DLMemberRecurse { $ ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- const 的全面总结
可以定义const常量 const int Max = 100; 2 便于进行类型检查 const常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类 ...
- thinkphp通行证服务,验证登录,注销登录
<?php /** * 通行证服务 */ class PassportService extends Service { /** * 验证用户或者管理员是否已登录 * @return boole ...
- (转) linux目录结构详细介绍
转自:http://yangrong.blog.51cto.com/6945369/1288072 目录 1.树状目录结构图 2./目录 3./etc/目录 4./usr/目录 5./var/目录 6 ...
- java解析xml禁止校验dtd
参考: http://shansun123.iteye.com/blog/1020425 http://blog.csdn.net/hailanzhijia/article/details/60049 ...
- PHP中如何获取多个checkbox的值
> > > weeks后的中括号不可漏,否则用PHP获取的时候只能取到最后一个值.之后PHP就很好处理了,如下: PHP获取checkbox值方法一: $weeks = $_POST ...
- 静态wenb开发,动态web开发