1 solutions
-
0
C++ :
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define MAXM 100010 #define INFF 10000000 using namespace std; int Ver, Edge; int first[MAXM]; int d[MAXM], u[MAXM], v[MAXM], w[MAXM], nextE[MAXM]; void read_graph() { scanf("%d%d", &Ver, &Edge); for(int i=1; i<=Ver; i++)first[i] = -1; //初始化表头 for(int e=1; e<=Edge; e++){ scanf("%d%d%d", &u[e], &v[e], &w[e]); nextE[e] = first[u[e]]; //插入链表 first[u[e]] = e; } return ; } void Bellman_Ford() { queue<int> q; bool inq[MAXM]; for(int i=1; i<=Ver; i++)d[i] = (i==1 ? 0 : INFF); memset(inq, 0, sizeof(inq));//“在队列中的”标志 q.push(1); while(! q.empty()) { int x = q.front(); q.pop(); inq[x] = false; //清除"在队列中的"标志 for(int e=first[x]; e!=-1; e=nextE[e]) if(d[v[e]] > d[x] + w[e]) { d[v[e]] = d[x] + w[e]; if(!inq[v[e]])//如果已经在队列中,就不要重复加了 { inq[v[e]] = true; q.push(v[e]); } } } for(int i=2; i<=Ver; i++){ cout<<d[i]<<endl; } return ; } int main() { read_graph(); Bellman_Ford(); return 0; }
- 1
Information
- ID
- 16329
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By