uvalive 6800 - The Mountain of Gold? 判负环
2016-08-25 11:54
381 查看
题意:给出一个图,判断能不能从0出发,再回到0,经过边的权值为负。
判断图中有没有负环,并且负环上的点能回到0即可。
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
int dist[maxn];
struct Edge{
int u, v, cost;
Edge(){}
Edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
};
vector<Edge> e;
vector<int> ee[1005];
int vis[1005];
int dfs(int u) {
if(u == 0)
return 1;
vis[u] = 1;
for(int i = 0; i < ee[u].size(); i++) {
if(!vis[ee[u][i]])
if(dfs(ee[u][i]))
return 1;
}
return 0;
}
bool bellman_ford(int s, int n){
for(int i = 0; i < n; ++i) dist[i] = INF;
dist[s] = 0;
for(int i = 1; i < n; ++i){
bool flag = false;
for(int j = 0; j < e.size(); ++j){
int u = e[j].u; int v = e[j].v;
int cost = e[j].cost;
if(dist[v] > dist[u] + cost){
dist[v] = dist[u] + cost;
flag = true;
}
}
if(!flag) return true;
}
for(int j = 0; j < e.size(); ++j) {
memset(vis, 0, sizeof(vis));
if(dfs(e[j].v) && dist[e[j].v] > dist[e[j].u] + e[j].cost)
return false;
}
return true;
}
int main()
{
int kase = 0;
int T; scanf("%d", &T);
while(T --){
e.clear();
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
ee[i].clear();
while(m --){
int u, v, cost;
scanf("%d%d%d",&u, &v, &cost);
ee[u].push_back(v);
e.push_back(Edge(u, v, cost));
}
printf("Case #%d: %s\n",++kase, !bellman_ford(0, n) ? "possible" : "not possible");
}
return 0;
}
判断图中有没有负环,并且负环上的点能回到0即可。
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
int dist[maxn];
struct Edge{
int u, v, cost;
Edge(){}
Edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
};
vector<Edge> e;
vector<int> ee[1005];
int vis[1005];
int dfs(int u) {
if(u == 0)
return 1;
vis[u] = 1;
for(int i = 0; i < ee[u].size(); i++) {
if(!vis[ee[u][i]])
if(dfs(ee[u][i]))
return 1;
}
return 0;
}
bool bellman_ford(int s, int n){
for(int i = 0; i < n; ++i) dist[i] = INF;
dist[s] = 0;
for(int i = 1; i < n; ++i){
bool flag = false;
for(int j = 0; j < e.size(); ++j){
int u = e[j].u; int v = e[j].v;
int cost = e[j].cost;
if(dist[v] > dist[u] + cost){
dist[v] = dist[u] + cost;
flag = true;
}
}
if(!flag) return true;
}
for(int j = 0; j < e.size(); ++j) {
memset(vis, 0, sizeof(vis));
if(dfs(e[j].v) && dist[e[j].v] > dist[e[j].u] + e[j].cost)
return false;
}
return true;
}
int main()
{
int kase = 0;
int T; scanf("%d", &T);
while(T --){
e.clear();
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
ee[i].clear();
while(m --){
int u, v, cost;
scanf("%d%d%d",&u, &v, &cost);
ee[u].push_back(v);
e.push_back(Edge(u, v, cost));
}
printf("Case #%d: %s\n",++kase, !bellman_ford(0, n) ? "possible" : "not possible");
}
return 0;
}
相关文章推荐
- UVAlive 6800 The Mountain of Gold?(负环判断)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- The Mountain of Gold? UVALive - 6800
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVALive - 6800 The Mountain of Gold?(Bellman-ford找负权回路,dfs)
- UVALive 6342 The Mirror of Galadriel (回文串)
- UVALive 6345 The Glittering Caves of Aglarond (找规律求最多)
- HDU 3666&& UVALive - 5094 THE MATRIX PROBLEM(stack判负环,差分约束)
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive - 7484 Association for the Country of Mububa(dp)
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive - 3523 Knights of the Round Table(无向图的双连通分量)
- UVALive - 3523 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- UVALive 3523 Knights of the Round Table(BCC+二分图)
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
- UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
- UVALive 7661 The Size of the Smallest Hole(dfs)
- UVALive - 3523 Knights of the Round Table(双联通分量)
- UVAlive 3523 Knights of the Round Table [点双连通分量] [Tarjan]
- UVALive - 3972 March of the Penguins(最大流+枚举)