您的位置:首页 > 其它

51nod 1445 变色DNA(最短路变形)

2017-08-19 21:14 429 查看
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=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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: