POJ - 2387:Til the Cows Come Home


Til the Cows Come Home
出典:POJ
ラベル:最短パス問題、Dijkstraアルゴリズム
参考資料:
類似タイトル:
タイトル
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it. Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
入力
  • Line 1: Two integers: T and N
  • Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.

  • しゅつりょく
  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

  • 入力サンプル
    5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
    出力サンプル
    90
    サンプル解釈
    INPUT DETAILS: There are five landmarks. OUTPUT DETAILS: Bessie can get home by following trails 4, 3, 2, and 1.
    テーマの大意
    N個のノード,T辺の無方向重み付け図,点Nから点1までの最短距離を計算する.
    リファレンスコード
    #include
    #include
    #include
    #include
    using namespace std;
    
    const int MAXN=1005; //   
    const int INF=0x3f3f3f3f;
    
    struct Dijkstra { //           。     ,       。
    	struct Edge {
    		int u; //      
    		int v; //       
    		int w; //    
    		Edge(int u,int v,int w):u(u),v(v),w(w) {}
    		bool operator < (const Edge &e) const {
    			return w>e.w;
    		}
    	};
    
    	struct Dist { //      u   w
    		int u;
    		int w;
    		Dist(int u,int w):u(u),w(w) {}
    		bool operator < (const Dist &d) const {
    			return w>d.w;
    		}
    	};
    
    	vector<Edge> adj[MAXN];
    	priority_queue<Dist> pq;
    	int distTo[MAXN];
    	bool vis[MAXN];
    
    	void dijkstra(int s) {
    		distTo[s]=0;
    		pq.push(Dist(s,0));
    		while(!pq.empty()) {
    			int u=pq.top().u; pq.pop();
    			if(vis[u]==true) continue;
    			relax(u);
    		}
    	}
    
    	void init() {
    		for(int i=0; i<MAXN; i++) adj[i].clear();
    		while(!pq.empty()) pq.pop();
    		memset(distTo,INF,sizeof(distTo));
    		memset(vis,0,sizeof(vis));
    	}
    
    	void addEdge(int u, int v, int w) {
    		adj[u].push_back(Edge(u,v,w));
    	}
    
    	void relax(int u) {
    		vis[u]=true;
    		for(int i=0; i<adj[u].size(); i++) {
    			int v=adj[u][i].v;
    			if(distTo[v]>distTo[u]+adj[u][i].w) {
    				distTo[v]=distTo[u]+adj[u][i].w;
    				pq.push(Dist(v,distTo[v]));
    			}
    		}
    	}
    
    };
    
    Dijkstra dijkstra;
    int T,N;
    
    int main() {
    	dijkstra.init();
    	int u,v,w;
    	scanf("%d%d",&T,&N);
    	for(int i=0; i<T; i++) {
    		scanf("%d%d%d",&u, &v, &w);
    		dijkstra.addEdge(u,v,w);
    		dijkstra.addEdge(v,u,w);
    	}
    	dijkstra.dijkstra(1);
    	printf("%d
    "
    ,dijkstra.distTo[N]); return 0; }