题意:给一个带权无向图,求图中能成环的权值最小值,如果不能成环,输出-1;
分析:
暴力+dijkstra。枚举每一条边,然后用dijkstra求s到t的距离(其中舍去s-t这条边)。
dijkstra找到t就跳出,或出队列的距离 >= 当前找到的最小距离 就跳出。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> P;
typedef long long LL;
const int N = 4e3+10;
const int INF = 0x3f3f3f3f;
struct edge{
int to, cost;
};
int V, min_;
vector<edge> G[2*N];
int d[N*2+1];
void dijkstra(int s, int t)
{
priority_queue<P, vector<P>, greater<P> > que;
fill(d, d+V, INF);
d[s] = 0;
que.push(P(0,s));
while(!que.empty()){
P p = que.top(); que.pop();
int v = p.second;
if(d[v] < p.first) continue;
if(v == t) break;
if(d[v] >= min_) break;
for(int i = 0; i < (int)G[v].size(); i++){
edge e = G[v][i];
if(v == s && e.to == t) continue;
if(d[e.to] > d[v] + e.cost){
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
map<P, int> mp;
struct node
{
int u, v, cost;
}Eg[N];
int main()
{
int cas = 1, T, m;
cin >> T;
while(T--)
{
int cnt = 1;
scanf("%d", &m);
int cost, x1, y1, x2, y2;
for(int i = 1; i <= 2*N; i++) G[i].clear();
mp.clear();
for(int i = 1; i <= m; i++){
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &cost);
int u, v;
if(mp[P(x1, y1)] == 0){
mp[P(x1, y1)] = cnt++;
}
if(mp[P(x2,y2)] == 0){
mp[P(x2,y2)] = cnt++;
}
u = mp[P(x1,y1)], v = mp[P(x2,y2)];
G[u].push_back(edge{v,cost});
G[v].push_back(edge{u,cost});
Eg[i].cost = cost;
Eg[i].u = u;
Eg[i].v = v;
}
min_ = INF;
V = cnt;
for(int i = 1; i <= m; i++){
dijkstra(Eg[i].u, Eg[i].v);
min_ = min(min_, d[Eg[i].v] + Eg[i].cost);
}
if(min_ == INF){
printf("Case #%d: 0\n", cas++);
}
else
printf("Case #%d: %d\n", cas++, min_);
}
return 0;
}