题意:给出一个有向图,每条边有两个属性:一个长度一个费用。费用可能是负数。长度一定是非负的。给出一个起点和一个终点,现要求,从起点走到终点,且从每个点走出时选择的那条边必须是以该点作为起点的边中费用最小或并列费用最小的边。如果依据这个原则无法走到终点则输出VOID。并且要求从起点到终点的过程中,先要保证费用最小,在有多解时,保证长度最小。最后输出最小费用和长度。如果费用可以无限小则输出UNBOUND。
分析:读入图后,可以先将所有点的出边进行一下整理,只保留费用最小的边。然后无论怎么走都一定复合第一条费用最小边原则。然后要判断费用是否可能无限小,即判断从起点到终点的路途中是否可能有负权回路。这是这道题的难点,我们之前只会用SPFA或bellman-ford全图是否有负权回路,那么怎么判断起点到终点之间有没有呢?既然是要走到终点,那么所有不能到达终点的点都是绝对不能去的,那么我们就先把这些点删除。然后剩下的所有点都是能到达终点的,现在只要图中含有负权回路则一定费用无限小。且如果本题费用如果无限小则现在图中一定含有负权回路。那么怎么判断哪些点能到达终点呢?只要建立一个与原图反向的图,再从终点开始dfs看能到达哪些点,这些点就是在原图中能到达终点的点。反向图即把原图中所有的边的起点变成终点,终点变成起点。
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; #define MAX_NODE_NUM 1505
#define INF 0x3f3f3f3f struct Edge
{
int v;
int weight;
int length;
Edge()
{}
Edge(int v, int length, int weight):v(v), length(length), weight(weight)
{}
}dist[MAX_NODE_NUM]; int node_num, road_num;
int origin, destination;
int least_fee[MAX_NODE_NUM];
bool vis[MAX_NODE_NUM];
int push_cnt[MAX_NODE_NUM]; vector<vector<Edge> > G;
vector<vector<Edge> > R; void addedge(vector<vector<Edge> > &G, int u, int v, int length, int weight)
{
G[u].push_back(Edge(v, length, weight));
} void input()
{
fill(least_fee, least_fee + node_num, INF);
G.clear();
G = vector<vector<Edge> > (node_num);
for (int i = ; i < road_num; i++)
{
int u, v, l, fuv, fvu;
char st[];
scanf("%s", st);
sscanf(st, "(%d,%d,%d[%d]%d)", &u, &v, &fuv, &l, &fvu);
addedge(G, u, v, l, fuv);
addedge(G, v, u, l, fvu);
least_fee[u] = min(least_fee[u], fuv);
least_fee[v] = min(least_fee[v], fvu);
}
} void clean_edge()
{
for (int i = ; i < node_num; i++)
{
int j = ;
for (vector<Edge>::iterator j = G[i].begin(); j != G[i].end();)
{
if (j->weight > least_fee[i])
j = G[i].erase(j);
else
j++;
}
}
} void reverse_graph()
{
R.clear();
R = vector<vector<Edge> > (node_num);
for (int i = ; i < node_num; i++)
{
for (vector<Edge>::iterator j = G[i].begin(); j != G[i].end(); j++)
addedge(R, j->v, i, j->length, j->weight);
}
} void dfs(int id)
{
if (vis[id])
return;
vis[id] = true;
for (vector<Edge>::iterator i = R[id].begin(); i != R[id].end(); i++)
dfs(i->v);
} void clean_node()
{
for (int i = ; i < node_num; i++)
{
if (!vis[i])
{
G[i].clear();
continue;
}
for (vector<Edge>::iterator j = G[i].begin(); j != G[i].end();)
{
if (!vis[j->v])
j = G[i].erase(j);
else
j++;
}
}
} bool relax(Edge &dist, int length, int weight)
{
if (dist.weight > weight || (dist.weight == weight && dist.length > length))
{
dist.weight = weight;
dist.length = length;
return true;
}
return false;
} void SPFA()
{
fill(dist, dist + node_num, Edge(-, INF, INF));
memset(push_cnt, , sizeof(push_cnt));
queue<Edge> q;
q.push(Edge(origin, , ));
dist[origin] = Edge(-, , );
bool unbound = false;
while (!q.empty())
{
Edge temp = q.front();
q.pop();
for (vector<Edge>::iterator i = G[temp.v].begin(); i != G[temp.v].end(); i++)
{
if (relax(dist[i->v], temp.length + i->length, temp.weight + i->weight))
{
q.push(Edge(i->v, dist[i->v].length, dist[i->v].weight));
push_cnt[i->v]++;
if (push_cnt[i->v] > node_num)
{
puts("UNBOUND");
return;
}
}
}
}
printf("%d %d\n", dist[destination].weight, dist[destination].length);
} int main()
{
while (scanf("%d%d%d%d", &node_num, &road_num, &origin, &destination) != EOF)
{
input();
clean_edge();
reverse_graph();
memset(vis, , sizeof(vis));
dfs(destination);
if (!vis[origin])
{
puts("VOID");
continue;
}
clean_node();
SPFA();
}
return ;
}