hdu2722 简单最短路,处理好输入就行
生活随笔
收集整理的這篇文章主要介紹了
hdu2722 简单最短路,处理好输入就行
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 從左上角走到右下角,有的最短時間,每段路徑的長度都是2520,每段上都有自己的限制速度,方向。
思路:
? ? ?直接寫就行了,就是個最短路,權值是2520/限制,輸入的時候細心點就行了。
#include<stdio.h> #include<string.h> #include<queue>#define N_node 500 + 50 #define N_edge 2000 + 200 #define INF 1000000000 using namespace std;typedef struct {int to ,next ,cost; }STAR;STAR E[N_edge]; int list[N_node] ,tot; int s_x[N_node] ,mark[N_node];void add(int a ,int b ,int c) {E[++tot].to = b;E[tot].cost = c;E[tot].next = list[a];list[a] = tot; }void Spfa(int s ,int n) {for(int i = 0 ;i <= n ;i ++)s_x[i] = INF ,mark[i] = 0;s_x[s] = 0;mark[s] = 1;queue<int>q;q.push(s);while(!q.empty()){int xin ,tou;tou = q.front();q.pop();mark[tou] = 0;for(int k = list[tou] ;k ;k = E[k].next){xin = E[k].to;if(s_x[xin] > s_x[tou] + E[k].cost){s_x[xin] = s_x[tou] + E[k].cost;if(!mark[xin]){mark[xin] = 1;q.push(xin);}}}}return ; } int main () {int i ,j ,n ,m ,num;char str[10];while(~scanf("%d %d" ,&n ,&m) && n + m){int mm = m + 1 ,nn = n + 1;memset(list ,0 ,sizeof(list)) ,tot = 1;for(i = 1 ;i <= nn ;i ++){for(j = 1 ;j <= m ;j ++){int now = (i - 1) * mm + j;scanf("%d %s" ,&num ,str);if(!num) continue;num = 2520 / num;if(str[0] == '*') add(now ,now + 1 ,num) ,add(now + 1 ,now ,num);if(str[0] == '>') add(now ,now + 1 ,num);if(str[0] == '<') add(now + 1 ,now ,num);}if(i <= n)for(j = 1 ;j <= mm ;j ++){scanf("%d %s" ,&num ,str);int now = (i - 1) * mm + j;if(!num) continue;num = 2520 / num;if(str[0] == '*') add(now ,now + mm ,num) ,add(now + mm ,now ,num);if(str[0] == '^') add(now + mm ,now ,num);if(str[0] == 'v') add(now ,now + mm ,num);}}Spfa(1 ,nn * mm);if(s_x[nn * mm] == INF) printf("Holiday\n");else printf("%d blips\n" ,s_x[nn * mm]);}return 0; }
總結
以上是生活随笔為你收集整理的hdu2722 简单最短路,处理好输入就行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu3035 最小割转换成最短路
- 下一篇: hdu 3721 树的最小直径