hdu1428 spfa+记忆化搜索
生活随笔
收集整理的這篇文章主要介紹了
hdu1428 spfa+记忆化搜索
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:題意坑爹,很容易誤認成是做短路的條數,題意是給你一個圖,讓你從起點走到終點,問你有多少種走法,但有一個限制,假如你想從a走到b,必須滿足終點到b的最短距離小于終點到a的最短距離.思路:先以終點為起點跑一便單元源最短路,然后記憶化搜索路徑條數就行了...
#include<stdio.h>
#include<string.h>
#include<queue>#define N_node 2500 + 500
#define N_edge 10000 + 1000
#define inf 9223372036854775807
using namespace std;typedef struct
{int to ,next;__int64 cost;
}STAR;STAR E[N_edge];
int list[N_node] ,tot;
__int64 s_x[N_node];void add(int a ,int b ,__int64 c)
{E[++tot].to = b;E[tot].cost = c;E[tot].next = list[a];list[a] = tot;
}void spfa(int s ,int n)
{int mark[N_node] = {0};mark[s] = 1;for(int i = 0 ;i <= n ;i ++)s_x[i] = inf;s_x[s] = 0;queue<int>q;q.push(s);while(!q.empty()){int tou ,xin;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);}}}}
}__int64 maxx[N_node];
int mark[N_node];
__int64 map[N_node][N_node];
__int64 dfs(int s ,int t)
{if(maxx[s] != 0) return maxx[s];__int64 sum = 0;for(int k = list[s] ;k ;k = E[k].next){int to = E[k].to;if(mark[to] || s_x[to] >= s_x[s]) continue;mark[to] = 1;sum += dfs(to ,t);mark[to] = 0;}maxx[s] = sum;return sum;
}int main ()
{int n ,i ,j;while(~scanf("%d" ,&n)){for(i = 1 ;i <= n ;i ++)for(j = 1 ;j <= n ;j ++)scanf("%I64d" ,&map[i][j]);memset(list ,0 ,sizeof(list));tot = 1;for(i = 1 ;i <= n ;i ++)for(j = 1 ;j <= n ;j ++){int now = (i - 1) * n + j;if(j < n) add(now ,now + 1 ,map[i][j+1]);if(j > 1) add(now ,now - 1 ,map[i][j-1]);if(i < n) add(now ,now + n ,map[i+1][j]);if(i > 1) add(now ,now - n ,map[i-1][j]);}add(0 ,1 ,map[1][1]);add(1 ,0 ,map[1][1]);spfa(n * n ,n * n);memset(maxx ,0 ,sizeof(maxx));memset(mark ,0 ,sizeof(mark));mark[0] = 1;maxx[n*n] = 1;printf("%I64d\n" ,dfs(0 ,n * n));}return 0;
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的hdu1428 spfa+记忆化搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu4396 多状态spfa
- 下一篇: hdu4118