标签:algo www done pac .com pre void stdin continue
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445
题意:
思路:
挺好的一道题目,如果$colormap[i][j]$为‘Y‘,那么这条边的代价就是前面Y出现的次数。也就是说前面必须得都破坏了这样才能轮到这条边,这样一来跑一遍最短路即可。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #include<vector> 7 #include<stack> 8 #include<queue> 9 #include<cmath> 10 #include<map> 11 #include<set> 12 using namespace std; 13 typedef long long ll; 14 const int INF = 0x3f3f3f3f; 15 const int maxn=3000+5; 16 const int mod=1e9+7; 17 18 int n; 19 int tot; 20 char s[55]; 21 int head[55]; 22 int d[55]; 23 bool done[55]; 24 25 struct node 26 { 27 int v,w,next; 28 }edge[maxn]; 29 30 struct HeapNode 31 { 32 int d,u; 33 HeapNode(){} 34 HeapNode(int d, int u):d(d),u(u){} 35 bool operator < (const HeapNode& rhs) const 36 { 37 return d>rhs.d; 38 } 39 }; 40 41 void addEdge(int u, int v, int w) 42 { 43 edge[tot].v=v; 44 edge[tot].w=w; 45 edge[tot].next=head[u]; 46 head[u]=tot++; 47 } 48 49 void dijkstra(int s) 50 { 51 priority_queue<HeapNode> Q; 52 for(int i=0;i<n;i++) d[i]=INF; 53 d[s]=0; 54 memset(done,0,sizeof(done)); 55 Q.push(HeapNode(0,s)); 56 while(!Q.empty()) 57 { 58 HeapNode x=Q.top(); Q.pop(); 59 int u=x.u; 60 if(done[u]) continue; 61 done[u]=true; 62 for(int i=head[u];i!=-1;i=edge[i].next) 63 { 64 int v=edge[i].v; 65 if(d[v]>d[u]+edge[i].w) 66 { 67 d[v]=d[u]+edge[i].w; 68 Q.push(HeapNode(d[v],v)); 69 } 70 } 71 } 72 } 73 74 int main() 75 { 76 //freopen("in.txt","r",stdin); 77 int T; 78 scanf("%d",&T); 79 while(T--) 80 { 81 tot=0; 82 memset(head,-1,sizeof(head)); 83 scanf("%d",&n); 84 for(int i=0;i<n;i++) 85 { 86 int cnt=0; 87 scanf("%s",s); 88 for(int j=0;j<n;j++) 89 if(s[j]==‘Y‘) {addEdge(i,j,cnt);cnt++;} 90 } 91 dijkstra(0); 92 if(d[n-1]==INF) puts("-1"); 93 else printf("%d\n",d[n-1]); 94 } 95 return 0; 96 }
标签:algo www done pac .com pre void stdin continue
原文地址:http://www.cnblogs.com/zyb993963526/p/7397960.html