这题用最短路径做,主要交易等级限制,就是交易链中任何两个人的等级差不能超过m,不然这条交易链是不成立的。
对于建图,如果物品 i 有可以降价的地方,假设为物品 j,那么 i 到 j 就有一条连边,值为那个交易额。而外地人原本就有一个到各个物品的边,值为物品原来的价值。
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
typedef struct Node {
int v,dis;
Node(){};
Node(int v,int dis):v(v),dis(dis){};
}Node;
const int MAXN = 110;
const int INF = 0x7f7f7f7f;
int level[MAXN];
int map[MAXN][MAXN];
bool vis[MAXN],use[MAXN];
int dis[MAXN];
int n,m;
void dijkstra(int low_level,int high_level);
bool operator <(const Node &a,const Node &b);
int main(int argc,char * argv[]) {
int i,j,k;
int x,y,z;
int ans;
while (scanf("%d%d",&m,&n)!=EOF) {
memset(map,0x7f,sizeof(map));
for (i=1;i<=n;++i) {
scanf("%d%d%d",&x,&level[i],&k);
map[i][n+1] = x;
for (j=1;j<=k;++j) {
scanf("%d%d",&y,&z);
map[i][y] = z;
}
}
ans = INF;
for (i=level[1]-m;i<=level[1];++i) {
dijkstra(i,i+m);
for (j=1;j<=n;++j)
ans = min(ans,map[j][n+1]+dis[j]);
}
printf("%d\n",ans);
}
return 0;
}
void dijkstra(int low_level,int high_level) {
int visnum,N,i;
Node cur;
priority_queue<Node> pq;
memset(vis,0,sizeof(vis));
memset(use,0,sizeof(use));
memset(dis,0x7f,sizeof(dis));
dis[1] = 0;
for (i=1;i<=n;++i) {
if (level[i] < low_level || level[i] > high_level) continue;
use[i] = 1;
}
while(!pq.empty()) pq.pop();
pq.push(Node(1,0));
visnum = 0;
while (!pq.empty()) {
cur = pq.top();
pq.pop();
if (vis[cur.v]) continue;
vis[cur.v] = 1;
for (i=1;i<=n;++i) {
if (INF == map[cur.v][i]) continue;
if (!use[i]) continue;
if (dis[cur.v] < dis[i] - map[cur.v][i]) {
dis[i] = dis[cur.v] + map[cur.v][i];
pq.push(Node(i,dis[i]));
}
}
}
}
bool operator <(const Node &a,const Node &b) {
return a.dis > b.dis;
}