Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1048 Accepted Submission(s): 530
Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
Sample Input
6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
Sample Output
42 -1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
Author
RoBa@TJU
Source
Recommend
lcy
题意是说有N个城市,M条路,从一个城市出发,终点必须返回该城市,要求必须走完所有的城市,让你求其最短路径。将所有城市连接一个源点,容量是1,费用是0.然后再将所有城市连接一个汇点,容量是1,费用是0,然后城市之间可以走的,连接起来,容量是1。如果是环的话那么入度等于出度。那么最大流等于顶点数n的时候,就有解了,否则输出-1.
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int sumFlow;
const int MAXN = 507;
const int MAXM = 10002;
const int INF = 1000000000;
struct Edge
{
int u, v, cap, cost;
int next;
} edge[MAXM<<2];
int NE;
int head[MAXN], dist[MAXN], pp[MAXN];
int m,n;
int g[MAXN][MAXN];
bool vis[MAXN];
void init()
{
NE = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[NE].u = u;
edge[NE].v = v;
edge[NE].cap = cap;
edge[NE].cost = cost;
edge[NE].next = head[u];
head[u] = NE++;
edge[NE].u = v;
edge[NE].v = u;
edge[NE].cap = 0;
edge[NE].cost = -cost;
edge[NE].next = head[v];
head[v] = NE++;
}
bool SPFA(int s, int t, int n)
{
int i, u, v;
queue <int> qu;
memset(vis,false,sizeof(vis));
memset(pp,-1,sizeof(pp));
for(i = 0; i <= n; i++) dist[i] = INF;
vis[s] = true;
dist[s] = 0;
qu.push(s);
while(!qu.empty())
{
u = qu.front();
qu.pop();
vis[u] = false;
for(i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].v;
if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
{
dist[v] = dist[u] + edge[i].cost;
pp[v] = i;
if(!vis[v])
{
qu.push(v);
vis[v] = true;
}
}
}
}
if(dist[t] == INF) return false;
return true;
}
int MCMF(int s, int t, int n) // minCostMaxFlow
{
int flow = 0; // 总流量
int i, minflow, mincost;
mincost = 0;
while(SPFA(s, t, n))
{
minflow = INF + 1;
for(i = pp[t]; i != -1; i = pp[edge[i].u])
if(edge[i].cap < minflow)
minflow = edge[i].cap;
flow += minflow;
for(i = pp[t]; i != -1; i = pp[edge[i].u])
{
edge[i].cap -= minflow;
edge[i^1].cap += minflow;
}
mincost += dist[t] * minflow;
}
sumFlow = flow; // 最大流
return mincost;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int u, v, c;
init();
int S=0;//S是源点
int T=2*n+1;//T是汇点
int a,b,cc;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&cc);
addedge(a,b+n,1,cc);
}
for(int i=1;i<=n;i++)
{
addedge(0,i,1,0);
addedge(n+i,T,1,0);
}
int ans = MCMF(S, T, T+1);//T+1是点的个数
if(sumFlow!=n)printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}