51nod 1445 变色DNA(最短路变形)
2017-08-19 21:14
429 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445
题意:
思路:
挺好的一道题目,如果$colormap[i][j]$为'Y',那么这条边的代价就是前面Y出现的次数。也就是说前面必须得都破坏了这样才能轮到这条边,这样一来跑一遍最短路即可。
题意:
思路:
挺好的一道题目,如果$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=3000+5; const int mod=1e9+7; int n; int tot; char s[55]; int head[55]; int d[55]; bool done[55]; 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=0;i<n;i++) d[i]=INF; d[s]=0; memset(done,0,sizeof(done)); Q.push(HeapNode(0,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!=-1;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=0; memset(head,-1,sizeof(head)); scanf("%d",&n); for(int i=0;i<n;i++) { int cnt=0; scanf("%s",s); for(int j=0;j<n;j++) if(s[j]=='Y') {addEdge(i,j,cnt);cnt++;} } dijkstra(0); if(d[n-1]==INF) puts("-1"); else printf("%d\n",d[n-1]); } return 0; }
相关文章推荐
- 51nod 1445:变色DNA 最短路变形
- 51nod 1445:变色DNA 最短路变形
- 51nod 1445:变色DNA(最短路变形)
- 51nod 1445:变色DNA 最短路变形
- 51nod 1445 变色DNA (最短路径变形)
- 51nod 1445 变色DNA【最短路SPFA+思维】
- 51nod 1445 变色DNA(最短路)
- 51NOD 1445 变色DNA(最短路)
- 变色DNA 51Nod - 1445 [最短路]
- 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)
- 51nod 1445 变色DNA问题(Dijkstra)
- 51Nod 1445 变色DNA
- 51NOD 1445 变色DNA
- 迷宫游戏 51Nod - 1459 【最短路变形】
- 51Nod 1445(考建图的最短路)
- 51nod1445 变色DNA【最短路】
- HDU 3790 最短路径问题 (最短路变形
- 最短路+记录路径+变形!!
- POJ - 1062 昂贵的聘礼 (最短路变形)
- poj 1797(最短路变形)