标签:工作 mis uri rom 帮助 targe other out clu
题目:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 83933 Accepted Submission(s): 36316
思路:
#include <iostream> #include <vector> using namespace std; const int size = 100+5; struct edge { int from; int to; int len; int other(int x) { if(x == from) return to; else return from; } }; vector<edge> graph[size];//存图 int dis[size];//保存到源点的距离 bool marked[size]; void init(int N)//初始化 { for(int i = 1; i <= N; i++) { graph[i].clear(); dis[i] = 0x3f3f3f3f;//到源点的距离为无穷大 marked[i] = false; } } void Dijkstra(int N) { //先用起始点来初始化 dis[1] = 0; marked[1] = true; for(int i = 0; i < graph[1].size(); i++) { if(graph[1][i].len < dis[graph[1][i].other(1)]) dis[graph[1][i].other(1)] = graph[1][i].len; } for(int i = 2; i <= N; i++)//还要连N-1个点 { int min = 0x3f3f3f3f; int pos; for(int i = 1; i <= N; i++)//找最小的未标记的边 { if(!marked[i] && dis[i] < min) { min = dis[i]; pos = i; } } marked[pos] = true; for(int i = 0; i < graph[pos].size(); i++)//以新点松弛边 { if(dis[pos]+graph[pos][i].len < dis[graph[pos][i].other(pos)]) dis[graph[pos][i].other(pos)] = dis[pos]+graph[pos][i].len; } } } int main() { int N, M; int a, b, c; while(cin >> N >> M && N != 0 || M != 0) { init(N); //输入 for(int i = 0; i < M; i++) { edge e; cin >> e.from >> e.to >> e.len; graph[e.from].push_back(e); graph[e.to].push_back(e); } Dijkstra(N); printf("%d\n", dis[N]); } return 0; }
标签:工作 mis uri rom 帮助 targe other out clu
原文地址:https://www.cnblogs.com/w-j-c/p/9218986.html