Han Solo wants to improve the reputation of the Millenium Falcon by completing more Kessel Runs! How does he complete these runs so fast? His secret is finding short cycles on the route and looping through them over and over, building up speed. Given a universe of galaxies, and 1 parsec routes that connect galaxies, find the shortest cycle in the universe.
Input Format
The first line contains two space-separated integers, and , the number of galaxies and the number of routes. Following this are lines, each describing a route. Each of these lines contains two space-separated integers and ; there is a one parsec route from vertex to vertex in the graph. These routes are one-way.
Constraints
Output Format
Print the minimum number of parsecs needed for Han to cycle. If there are no cycles in this universe, print -1
.
Sample Input 0
6 7
1 2
2 3
3 4
3 1
4 5
5 6
6 3
Sample Output 0
3
Sample Input 1
4 3
1 2
2 3
2 4
Sample Output 1
-1
#include
#define INF 0x3f3f3f
using namespace std;
int mp[1005][1005];
int dis[1005][1005];
int n,m;
void init()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
{
mp[i][j]=0;
dis[i][j]=0;
}
else
{
mp[i][j]=INF;
dis[i][j]=INF;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0;i1)
{
mp[x][y]=1;
dis[x][y]=1;
}
}
int ans=INF;
for(int k=1;k<=n;k++)
{
for(int i=1;i
#include
#include
#include
#include
constexpr int MAX_N = 1005;
using namespace std;
int n, m;
vector adj[MAX_N];
bool vis[MAX_N];
int solve(int s) {
memset(vis, 0, sizeof(vis));
queue to_visit;
to_visit.push(s);
int lvl = 0;
while (!to_visit.empty()) {
int sz = to_visit.size();
for (int i = 0; i < sz; ++i) {
int u = to_visit.front(); to_visit.pop();
if (u == s && lvl > 0) return lvl;
if (vis[u]) continue;
vis[u] = true;
for (int v : adj[u]) {
to_visit.push(v);
}
}
++lvl;
}
return -1;
}
int main() {
scanf(" %d %d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf(" %d %d", &u, &v);
adj[u].push_back(v);
}
int best = -1;
for (int u = 1; u <= n; ++u) {
int cur = solve(u);
if (cur != -1) {
if (best == -1 || cur < best) {
best = cur;
}
}
}
printf("%d\n", best);
return 0;
}
#include
#include
#include
#include
constexpr int MAX_N = 1005;
using namespace std;
int n, m;
vector adj[MAX_N];
bool vis[MAX_N];
int solve(int s) {
memset(vis, 0, sizeof(vis));
queue to_visit;
to_visit.push(s);
int lvl = 0;
while (!to_visit.empty()) {
int sz = to_visit.size();
for (int i = 0; i < sz; ++i) {
int u = to_visit.front(); to_visit.pop();
if (u == s && lvl > 0) return lvl;
if (vis[u]) continue;
vis[u] = true;
for (int v : adj[u]) {
to_visit.push(v);
}
}
++lvl;
}
return -1;
}
int main() {
scanf(" %d %d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf(" %d %d", &u, &v);
adj[u].push_back(v);
}
int best = -1;
for (int u = 1; u <= n; ++u) {
int cur = solve(u);
if (cur != -1) {
if (best == -1 || cur < best) {
best = cur;
}
}
}
printf("%d\n", best);
return 0;
}
暴力bfs只能用于环边长为1的情况 注意
参考 http://blog.163.com/acm_candice/blog/static/168796081201010174751374/