Pandaland
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 105 Accepted Submission(s): 14
Problem Description
Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can be treated as a point on a 2D plane. Different cities are located in different locations.
There are also M bidirectional roads connecting those cities. There is no intersection between two distinct roads except their endpoints. Besides, each road has a cost w.
One day, Mr. Panda wants to find a simple cycle with minmal cost in the Pandaland. To clarify, a simple cycle is a path which starts and ends on the same city and visits each road at most once.
The cost of a cycle is the sum of the costs of all the roads it contains.
There are also M bidirectional roads connecting those cities. There is no intersection between two distinct roads except their endpoints. Besides, each road has a cost w.
One day, Mr. Panda wants to find a simple cycle with minmal cost in the Pandaland. To clarify, a simple cycle is a path which starts and ends on the same city and visits each road at most once.
The cost of a cycle is the sum of the costs of all the roads it contains.
Input
The first line of the input gives the number of test cases, T. T test cases follow.
Each test case begins with an integer M.
Following M lines discribes roads in Pandaland.
Each line has 5 integers x1,y1,x2,y2, w, representing there is a road with cost w connecting the cities on (x1,y1) and (x2,y2).
Each test case begins with an integer M.
Following M lines discribes roads in Pandaland.
Each line has 5 integers x1,y1,x2,y2, w, representing there is a road with cost w connecting the cities on (x1,y1) and (x2,y2).
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the cost Mr. Panda wants to know.
If there is no cycles in the map, y is 0.
∙1≤T≤50.
∙1≤m≤4000.
∙−10000≤xi,yi≤10000.
∙1≤w≤105.
If there is no cycles in the map, y is 0.
limits
∙1≤T≤50.
∙1≤m≤4000.
∙−10000≤xi,yi≤10000.
∙1≤w≤105.
Sample Input
2 5 0 0 0 1 2 0 0 1 0 2 0 1 1 1 2 1 0 1 1 2 1 0 0 1 5 9 1 1 3 1 1 1 1 1 3 2 3 1 3 3 2 1 3 3 3 1 1 1 2 2 2 2 2 3 3 3 3 1 2 2 1 2 2 1 3 2 4 1 5 1 4
Sample Output
Case #1: 8 Case #2: 4
题意:给一个图求图中一个权值最小的环
解:一开始 以为要使每一个点都遍历一遍,但题目只要求一个环就可以了
这题居然是个暴力 枚举每一条边 使这条边为环的两个端点 然后跑最短路。。。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
const int N =10000+10;
typedef long long LL;
typedef pair<int,int>pi;
map<pi,int>q;
const int inf = 0x3f3f3f3f;
struct node1
{
int x, y, w;
}g[N];
struct node
{
int to, w, id, next;
}p[N*2];
int head[N], cnt;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
return ;
}
void add(int u,int v,int id,int w)
{
p[cnt].to=v,p[cnt].w=w,p[cnt].id=id;p[cnt].next=head[u];head[u]=cnt++;
p[cnt].to=u,p[cnt].w=w,p[cnt].id=id;p[cnt].next=head[v];head[v]=cnt++;
return ;
}
int xid,wid, ans, dist[N];
int vis[N];
int dij(int s,int e,int n)
{
priority_queue<pi,vector<pi>,greater<pi> >qx;
for(int i=1;i<=n;i++) dist[i]=inf,vis[i]=0;
dist[s]=0;
while(!qx.empty()) qx.pop();
qx.push(make_pair(0,s));
while(!qx.empty())
{
int u=qx.top().second,w=qx.top().first;
qx.pop();
if(w+wid>=ans) break;
if(vis[u]||dist[u]<w) continue;
vis[u]=1;
for(int i=head[u];i!=-1;i=p[i].next)
{
int v=p[i].to;
if(p[i].id==xid) continue;
if(dist[v]>w+p[i].w)
{
dist[v]=w+p[i].w;
qx.push(make_pair(dist[v],v));
}
}
}
return dist[e];
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
init();
int m;
scanf("%d", &m);
q.clear();
int k=1;
for(int i=0;i<m;i++)
{
int a, b, c, d, w;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &w);
if(q[make_pair(a,b)]==0) q[make_pair(a,b)]=k++;
if(q[make_pair(c,d)]==0) q[make_pair(c,d)]=k++;
g[i].x=q[make_pair(a,b)],g[i].y=q[make_pair(c,d)],g[i].w=w;
add(q[make_pair(a,b)],q[make_pair(c,d)],i,w);
}
ans=inf;
for(int i=0;i<m;i++)
{
int s=g[i].x,e=g[i].y;
xid=i,wid=g[i].w;
int sum=dij(s,e,k);
ans=min(ans,sum+g[i].w);
}
if(ans>=inf)ans=0;
printf("Case #%d: %d\n",ncase++,ans);
}
return 0;
}