#include
#define sf scanf
#define pf printf
#define inf 1e18
#define INF 1000000000
#define MAX_N 5001
#define MAX_E 25000001
using namespace std;
typedef long long lld;
int v, e;
int dist[MAX_N];
struct Edge
{
int x, y, weight;
Edge(int _x, int _y, int _weight){
x = _x;
y = _y;
weight = _weight;
}
};
vector E;
int BellmanFord(int source)
{
for (int i=0;i {
if (i == source) dist[i]=0;
else dist[i] = INF;
}
bool done = false;
for (int i=0;!done&&i {
done = true;
for (int j=0;j {
int so = E[j].x;
int de = E[j].y;
if (dist[so] + E[j].weight < dist[de])
{
dist[de] = dist[so] + E[j].weight;
done=false;
}
}
}
if (!done) return -1;
return 0;
}
int main()
{
int t;
cin >> t;
while(t--){
E.clear();
v , e;
cin >> v >> e;
for(int i=0; i int u , v , w;
cin >> u >> v >> w;
E.push_back(Edge(u,v,w));
}
BellmanFord(0) == -1 ? cout<<"possible\n":cout<<"not possible\n";
}
return 0;
#define sf scanf
#define pf printf
#define inf 1e18
#define INF 1000000000
#define MAX_N 5001
#define MAX_E 25000001
using namespace std;
typedef long long lld;
int v, e;
int dist[MAX_N];
struct Edge
{
int x, y, weight;
Edge(int _x, int _y, int _weight){
x = _x;
y = _y;
weight = _weight;
}
};
vector
int BellmanFord(int source)
{
for (int i=0;i
if (i == source) dist[i]=0;
else dist[i] = INF;
}
bool done = false;
for (int i=0;!done&&i
done = true;
for (int j=0;j
int so = E[j].x;
int de = E[j].y;
if (dist[so] + E[j].weight < dist[de])
{
dist[de] = dist[so] + E[j].weight;
done=false;
}
}
}
if (!done) return -1;
return 0;
}
int main()
{
int t;
cin >> t;
while(t--){
E.clear();
v , e;
cin >> v >> e;
for(int i=0; i
cin >> u >> v >> w;
E.push_back(Edge(u,v,w));
}
BellmanFord(0) == -1 ? cout<<"possible\n":cout<<"not possible\n";
}
return 0;
}
No comments:
Post a Comment