51nod 1445 变色DNA(最短路变形)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445
题意:

思路:
挺好的一道题目,如果$colormap[i][j]$为'Y',那么这条边的代价就是前面Y出现的次数。也就是说前面必须得都破坏了这样才能轮到这条边,这样一来跑一遍最短路即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn=+;
const int mod=1e9+; int n;
int tot;
char s[];
int head[];
int d[];
bool done[]; struct node
{
int v,w,next;
}edge[maxn]; struct HeapNode
{
int d,u;
HeapNode(){}
HeapNode(int d, int u):d(d),u(u){}
bool operator < (const HeapNode& rhs) const
{
return d>rhs.d;
}
}; void addEdge(int u, int v, int w)
{
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
} void dijkstra(int s)
{
priority_queue<HeapNode> Q;
for(int i=;i<n;i++) d[i]=INF;
d[s]=;
memset(done,,sizeof(done));
Q.push(HeapNode(,s));
while(!Q.empty())
{
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]>d[u]+edge[i].w)
{
d[v]=d[u]+edge[i].w;
Q.push(HeapNode(d[v],v));
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
tot=;
memset(head,-,sizeof(head));
scanf("%d",&n);
for(int i=;i<n;i++)
{
int cnt=;
scanf("%s",s);
for(int j=;j<n;j++)
if(s[j]=='Y') {addEdge(i,j,cnt);cnt++;}
}
dijkstra();
if(d[n-]==INF) puts("-1");
else printf("%d\n",d[n-]);
}
return ;
}
51nod 1445 变色DNA(最短路变形)的更多相关文章
- 51nod 1445 变色DNA(dij)
题目链接:51nod 1445 变色DNA 看了相关讨论再去用最短路:val[i][j]之间如果是'Y',说明i可以到达j,并且i到达j的代价是i那行 1到j-1 里面'Y'的数量. 最后,求 0到n ...
- 51nod 1445:变色DNA 最短路变形
1445 变色DNA 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有一只特别的狼,它在每个夜晚会进行变色,研究发现 ...
- 51NOD 1445 变色DNA
1445 变色DNA 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1,2...N-1.研究发现这只狼的基因中存在一个变色矩阵,记为colormap,如果 ...
- 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)
1445 变色DNA 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1 ...
- 51nod_1445 变色DNA 最短路模板 奇妙思维
这是一道最短路模板题,但是在理解题意和提出模型的阶段比较考验思维,很容易想到并且深深进入暴力拆解题目的无底洞当中. 题意是说:给出一个邻接矩阵,在每个点时,走且仅走向,合法路径中编号最小的点.问题是是 ...
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
随机推荐
- Debugging golang programs
https://ttboj.wordpress.com/2016/02/15/debugging-golang-programs/ I’ve been writing a lot of golang ...
- bootstrap modal插件弹出窗口如何限制最大高度,并且在内容过多时可以滚动显示
.modal-body{ max-height:400px; overflow-y:auto; } 只有在modal-body类上限制高度才能起作用,其他地方的限制均不起作用
- bootstrap模态框手动开启关闭与设置点击外部不关闭
http://www.cnblogs.com/qlqwjy/p/7491054.html 完整的参考菜鸟教程:http://www.runoob.com/bootstrap/bootstrap-mod ...
- 梯度下降法实现-python[转载]
转自:https://www.jianshu.com/p/c7e642877b0e 梯度下降法,思想及代码解读. import numpy as np # Size of the points dat ...
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- testng入门教程16数据驱动(把数据写在xml)
testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...
- thymeleaf 配置
thymeleaf,官网文档中,那个配置有误(估计是代码更新了但是文档没有更新).应该是这样的- <bean id="templateResolver" class=&quo ...
- python在交互模式下直接输入对象后回车,调用的是对象的__repr__()方法,这个方法表示的是一个编码,用print+对象是调用对象的__str__方法
交互模式下调用对象的__repr__()方法,这个方法表示的是一个编码 >>> u"国庆节快乐"u'\u56fd\u5e86\u8282\u5feb\u4e50' ...
- Linux基础命令---unzip
unzip 解压zip指令压缩过的文件.unzip将列出.测试或从ZIP存档中提取文件,这些文件通常在MS-DOS系统中找到.默认行为(没有选项)是将指定ZIP存档中的所有文件提取到当前目录(及其下面 ...
- python之路----黏包的解决方案
黏包的解决方案 远程执行命令 # server 下发命令 给client import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) ...