Uva10986-Sending email

1314 단어 최단로uva
최단거리 물문제.
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <ctype.h>
#define INF 1000000000
using namespace std;

struct edge{
	int next;
	int len;
};

int ans[20010];

int main(){
	int N;
	cin>>N;
	int count=0;
	while(N--){
		memset(ans,0,sizeof(ans));
		vector<edge> E[20010];
		int n,m,S,T;
		cin>>n>>m>>S>>T;
		
		int u,v,len;
		for(int i=0;i<m;i++){
			scanf("%d%d%d",&u,&v,&len);
			edge e1;e1.next=v;e1.len=len;
			edge e2;e2.next=u;e2.len=len;
			E[u].push_back(e1);
			E[v].push_back(e2);
		}
		
		queue<int> que;
		que.push(S);
		while(!que.empty()){
			int cur=que.front();que.pop();
			for(int i=0;i<E[cur].size();i++){
				if( (ans[cur]+E[cur][i].len)<ans[E[cur][i].next]||ans[E[cur][i].next]==0 ){
					ans[E[cur][i].next]=(ans[cur]+E[cur][i].len);
					que.push(E[cur][i].next);
				}
			}
		}
		count++;
		if(ans[T]){
			printf("Case #%d: %d
",count,ans[T]); }else{ printf("Case #%d: unreachable
",count); } } return 0; }

좋은 웹페이지 즐겨찾기