Shirlies
宁静专注认真的程序媛~

表示很无语的一题,纯粹意义上的字符处理,晕...

用优先队列做的,时间0ms...

代码如下:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 struct node
  7 {
  8     int u,v;
  9     int w;
 10 }p[5000];
 11 int d[1000];
 12 int n,m;
 13 int num;
 14 int first[5000];
 15 int nextt[5000];
 16 typedef pair<int,int> pii;
 17 
 18 void insert(int u,int v,int w)
 19 {
 20     p[num].u = u;
 21     p[num].v = v;
 22     p[num].w = w;
 23     nextt[num] = first[u];
 24     first[u] = num ++;
 25 }
 26 
 27 void dij()
 28 {
 29     memset(d,-1,sizeof(d));
 30     d[1] = 0;
 31     priority_queue<pii,vector<pii>,greater<pii> > q;
 32     q.push(make_pair(d[1],1));
 33     while(!q.empty())
 34     {
 35         pii u = q.top();
 36         q.pop();
 37         int x = u.second;
 38         if(d[x] != u.first)
 39             continue;
 40         for(int i = first[x];i != -1;i = nextt[i])
 41         {
 42             if(d[p[i].v] == -1 || d[p[i].v] > d[x] + p[i].w)
 43             {
 44                 d[p[i].v] = d[x] + p[i].w;
 45                 q.push(make_pair(d[p[i].v],p[i].v));
 46             }
 47         }
 48     }
 49     int aim = (n + 1) * (m + 1);
 50     if(d[aim] == -1)
 51         printf("Holiday\n");
 52     else
 53         printf("%d blips\n",d[aim]);
 54 }
 55 
 56 int main()
 57 {
 58     while(scanf("%d%d",&n,&m),n||m)
 59     {
 60         memset(first,-1,sizeof(first));
 61         num = 0;
 62         for(int i = 0;i < 2 * n + 1;i ++)
 63         {
 64             int k = i / 2 * (m + 1) + 1;//记录每行起始的编号,我是按顺序来一个一个编号的,譬如第一行是1---m+1,然后就按顺序咯
 65             int dd;
 66             char ss[5];
 67             if(i % 2 == 0)
 68             {
 69                 for(int j = 0;j < m;j ++)
 70                 {
 71                     scanf("%d%s",&dd,ss);
 72                     if(dd == 0)
 73                         continue;
 74                     dd = 2520 / dd;
 75                     if(ss[0] == '>')
 76                     {
 77                         insert(k + j,k + j + 1,dd);
 78                     }
 79                     else if(ss[0] == '<')
 80                     {
 81                         insert(k + j + 1,k + j,dd);
 82                     }
 83                     else 
 84                     {
 85                         insert(k + j,k + j + 1,dd);
 86                         insert(k + j + 1,k + j,dd);
 87                     }
 88                 }
 89             }
 90             else
 91             {
 92                 for(int j = 0;j < m + 1;j ++)
 93                 {
 94                     scanf("%d%s",&dd,ss);
 95                     if(dd == 0)
 96                         continue;
 97                      dd = 2520 / dd;
 98                     if(ss[0] == 'v')
 99                     {
100                         insert(k + j ,k + m + 1 + j,dd);
101                     }
102                     else if(ss[0] == '^')
103                     {
104                         insert(k + m + 1 + j,k + j,dd);
105                     }
106                     else 
107                     {
108                         insert(k + j ,k + m + 1 + j,dd);
109                         insert(k + m + 1 + j,k + j,dd);
110                     }
111                 }
112             }
113         }
114 
115         dij();
116     }
117 
118     return 0;
119 }

 

posted on 2012-05-04 21:13  Shirlies  阅读(344)  评论(0编辑  收藏  举报