这叫……分层建图?
n <= 50,最多50*(50 - 1) = 2450条边,m <= 10,把所有可能经过的边都建出来最差24500条
密集图,所以跑dij……
也可能是codevs数据水所以过了……
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define INF 2147483647
using namespace std;
const int MAXN = 10000 + 50;
struct zt
{
int num;
double dis;
int cost;
};
int n,m;
double x,dis[MAXN][101];
int head[MAXN],next[MAXN << 1],tot;
struct edge
{
int f,t;
double v;
int cost;
}l[MAXN << 1];
void init(int n)
{
for(int i = 1;i <= n;i ++)
{
head[i] = -1;
for(int j = 0;j <= 100;j ++)
dis[i][j] = INF;
}
}
void build(int f,int t,double v,int cost)
{
l[++ tot] = (edge){f,t,v,cost};
next[tot] = head[f];
head[f] = tot;
}
void solve(int f,int t,double v)
{
int cost = 0;
build(f,t,v,0);
build(t,f,v,0);
for(cost = 1;cost <= m;cost ++)
{
v /= 2;
build(f,t,v,cost);
build(t,f,v,cost);
}
}
bool operator < (zt a,zt b)
{
return a.dis > b.dis;
}
priority_queue <zt> q;
bool used[MAXN][101];
void dij(int x)
{
dis[x][0] = 0;
used[x][0] = true;
q.push((zt){x,0,0});
while(!q.empty())
{
zt u = q.top();
q.pop();
used[u.num][u.cost] = true;
if(u.num == n && u.cost == m)return;
for(int i = head[u.num];i != -1;i = next[i])
{
int t = l[i].t;
if(dis[t][u.cost + l[i].cost] > dis[u.num][u.cost] + l[i].v && u.cost + l[i].cost <= m)
{
dis[t][u.cost + l[i].cost] = dis[u.num][u.cost] + l[i].v;
q.push((zt){t,dis[t][u.cost + l[i].cost],u.cost + l[i].cost});
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
init(n);
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= n;j ++)
{
scanf("%lf",&x);
if(!x)continue;
solve(i,j,x);
}
}
dij(1);
printf("%.2lf",dis[n][m]);
return 0;
}

本文介绍了一种分层建图的方法,并通过Dijkstra算法解决了一个具体问题。该方法适用于密集图的情况,能够有效地处理最多50个节点、24500条边的复杂情况。
586

被折叠的 条评论
为什么被折叠?



