5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0
2 4
沒有過英語四級的渣渣 果然不能做這道題。。
理解為求最短路徑的個數了 。主要就是看這句話He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. 大概意思就是 如果A-B有路 並且我想從A走到B那麼有條件就是A到2的距離要大於B到2的距離
思路:
1.digkstra算法求出各點到2-1的最短距離 同時存貯其它點到2的最短距離
2.記憶化搜索 從1開始 找到符合條件的路徑數
#include <stdio.h>
#include <vector>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int min_path;
int min_count;
//存貯當前點到2的最短路徑
int dis[1005];
//記憶化搜索時存貯路徑數
int dp[1005];
int n;
//digkstra算法和記憶化搜索時 判斷是否已經走過某點
bool vis[1005];
struct node
{
int pos;
int cost;
bool friend operator<(node x,node y)
{
return x.cost>y.cost;
}
};
//存貯邊
vector<node>map[1005];
int digkstra(int x)
{
priority_queue<node>s;
memset(vis,false,sizeof(vis));
memset(dis,100,sizeof(dis));
node temp,temp1;
temp.pos=x;temp.cost=0;
s.push(temp);
while(!s.empty())
{
temp=temp1=s.top();s.pop();
vis[temp.pos]=true;
dis[temp.pos]=min(dis[temp.pos],temp.cost);
if(temp.pos==1)
return temp.cost;
for(int i=0;i<map[temp.pos].size();i++) i="0;i<map[pos].size();i++)" int="" pos="=2)" pos-="" result="0;" return="" temp="temp1;" temp.pos="x;" x="map[temp.pos][i].pos;" y="map[temp.pos][i].cost;">x有路 並且滿足pos到2的最短路徑大於x到2的最短路徑
if(!vis[x]&&dis[pos]>dis[x])
{
vis[x]=true;
result+=dfs(x,sum+y);
vis[x]=false;
}
}
dp[pos]=result;
return dp[pos];
}
int main()
{
while(~scanf("%d",&n)&&n)
{
int m;
scanf("%d",&m);
memset(map,0,sizeof(map));
for(int i=0;i<m;i++) d="" int="" min_count="0;" min_path="digkstra(2);" node="" pre="" return="" temp.pos="b;temp.cost=x;"><p>
</p><p>
</p>
</m;i++)></map[temp.pos].size();i++)></node></node></algorithm></queue></string.h></vector></stdio.h>