51nod1326 遥远的旅途(spfa+dp)
题意:
给出一个无向图,问从1到n是否存在一条长度为L的路径。
n,m<=50,1<=路径长度<=10000,L<=10^18
思路:
改变一下思路,我们发现,假设从起点1走到终点N有一条路径的长度为a,假设它再往一条与终点相连的长为b的路径反复走无数次后使得路径长度到达了T,那么一定有(T-a)%(2*k)==0,即**T%(2*k)=a%(2*k),所以我们只需要看是否从1到N存在一条路径长度为d,使得d%(2*k)=t%(2*k)
因为这个题目和模数有关,所以我们要把取摸的结果写入状态.
dp[i][j]表示处于i节点,从一号点到i号点的花费和%(2*k)(选择的边权)等与k的最小花费.
那么转移就是: dp[to][(j+quan)%mod]=min(dp[now][j]+quan)
因为具有后效性,所以需要spfa.
//这道题较多的参考了晚上的解法,在看懂之后自己又写了一遍
//链接:https://blog.csdn.net/qq_33229466/article/details/77131289
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
typedef long long LL; const int maxn = + , maxm = 2e4 + ;
int n, m, num, head[maxn];
LL t, dp[maxn][maxm], inf = ;
bool vis[maxn][maxm]; struct node {
int to, w, next;
}edge[maxn * ];
queue<pair<int, int>>q; void add_edge(int u, int v, int w)
{
edge[++num].to = v;
edge[num].w = w;
edge[num].next = head[u];
head[u] = num;
} void spfa(int mod)
{
q.push(make_pair(,)); vis[][] = ;
while (!q.empty())
{
pair<int,int> u = q.front(); q.pop();
int x = u.first, y = u.second;
for (int i = head[x]; i; i = edge[i].next)
if (dp[x][y] + edge[i].w < dp[edge[i].to][(y + edge[i].w) % mod])
{
int nx = edge[i].to, ny = (y + edge[i].w) % mod;
dp[nx][ny] = dp[x][y] + edge[i].w;
if (!vis[nx][ny]) {
q.push(make_pair(nx, ny));
vis[nx][ny] = ;
}
}
vis[x][y] = ;
}
} int main()
{
int kase;
scanf("%d", &kase);
while (kase--)
{
memset(head, , sizeof(head));
scanf("%d%d%lld", &n, &m, &t);
num = ;
for (int i = ; i <= m; i++)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
add_edge(x, y, w);
add_edge(y, x, w);
}
int flag = ;
for (int i = ; i <= num; i += )
if (edge[i].to == || edge[i + ].to == )
{
int w = edge[i].w * ;
for (int j = ; j <= n; j++) {
for (int k = ; k < w; k++) {
dp[j][k] = inf;
}
}
dp[][] = ;
spfa(w);
if (dp[n][t%w] <= t)
{
printf("Yes\n");
flag = ;
break;
}
}
if (!flag) printf("No\n");
}
return ;
}
51nod1326 遥远的旅途(spfa+dp)的更多相关文章
- 51nod 1326 奇妙的spfa+dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1326 1326 遥远的旅途 题目来源: TopCoder 基准时间限制: ...
- 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- HDU 3499 Flight spfa+dp
Flight Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Subm ...
- BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP
题意:链接 方法:SPFA+DP 解析:挺好的题目.因为数据范围较小所以用这样的方式能够搞,只是也是挺不好想的. 我们定义cost(i,j)表示从第i天走到第j天运用同一种方式的最小花费,然后因为数据 ...
- BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=2763 Description Alice和Bob现在要乘飞机旅行,他们选择了一家 ...
- BZOJ 1003 物流运输 题解 【SPFA+DP】
BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...
- HDU 4433 locker(SPFA+DP)
题目链接 去年区域赛的题目,早就看过题目了,又是过了好久了... 这题状态转移,一看就知道应该是 线性的那种,不过细节真的不好处理,一直没想出怎么搞,期间也看过题解,好像没太看懂... dp[i][j ...
- BZOJ-1003 物流运输trans SPFA+DP
傻逼错误耗我1h,没给全范围坑我1A.... 1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MB Submit: 529 ...
- [ZJOI2006]物流运输 SPFA+DP
题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...
随机推荐
- iptbales 允许访问vsftp
1.允许20 21 端口iptables -I INPUT -p tcp -m multiport --dport 20,21 -j ACCEPT 2.允许关联包通过iptables -A INPUT ...
- Vim 安装和配置、优化
Vim 介绍 Vim 官网:http://www.vim.org/ Vim 安装 CentOS:sudo yum install -y vim Ubuntu:sudo apt-get install ...
- springboot 基于Tomcate的自启动流程
Springboot 内置了Tomcat的容器,我们今天来说一下Springboot的自启动流程. 一.Spring通过注解导入Bean大体可分为四种方式,我们主要来说以下Import的两种实现方法: ...
- [Python-memcached]Python操作memcached
安装python-memchached插件 pip install python-memcached Collecting python-memcached Downloading python_me ...
- js—求数组中的最大最小值
参考链接:https://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html Math.min.appl ...
- codewars--js--Simple string expansion+ repeat(),includes()方法
问题描述: Consider the following expansion: solve("3(ab)") = "ababab" -- "ab&qu ...
- c# 匿名方法(函数) 匿名委托 内置泛型委托 lamada
匿名方法:通过匿名委托 .lamada表达式定义的函数具体操作并复制给委托类型: 匿名委托:委托的一种简单化声明方式通过delegate关键字声明: 内置泛型委托:系统已经内置的委托类型主要是不带返回 ...
- Python requests 调Jenkins登录后的接口,返回403Fobidden的原因及解决方法。
因Jenkins启用“防止跨站点请求伪造" 解决方法: 在Manage Jenkins->Configure Global Security 设置中将“防止跨站点请求伪造”取消勾选
- MySql学习-1.MySql的安装:
1.安装包的下载(mysql-v5.7.25 )(NavicatforMySQL_11.2.15): 链接:https://pan.baidu.com/s/166hyyYd3DMjYhMwdW805F ...
- Java数据结构--双向链表的实现
#java学习经验总结------双向链表的实现 双向链表的建立与单链表类似,只是需要使用pre指针指向前一个结点,并且在删除添加时不仅仅考虑next package datastructure; p ...