您的位置:首页 > 其它

hdoj 2112 HDU Today

2013-09-26 01:12 211 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112

解题思路:SPFA 算法(邻接矩阵)

/**************************************************************************
user_id: SCNU20102200088
problem_id: hdoj 2112
problem_name: HDU Today
**************************************************************************/

#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

//线段树
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

//手工扩展栈
#pragma comment(linker,"/STACK:102400000,102400000")

const double EPS=1e-9;
const double PI=acos(-1.0);
const double E=2.7182818284590452353602874713526;  //自然对数底数
const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)

const int x4[]={-1,0,1,0};
const int y4[]={0,1,0,-1};
const int x8[]={-1,-1,0,1,1,1,0,-1};
const int y8[]={0,1,1,1,0,-1,-1,-1};

typedef long long LL;

typedef int T;
T max(T a,T b){ return a>b? a:b; }
T min(T a,T b){ return a<b? a:b; }
T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
T lcm(T a,T b){ return a/gcd(a,b)*b; }

///////////////////////////////////////////////////////////////////////////
//Add Code:
const int maxn=155;
const int INF=1000000;
int num,dist[maxn],f[maxn][maxn];
bool visit[maxn];

void SPFA(int k){
int i;
for(i=1;i<=num;i++){
dist[i]=INF;
visit[i]=0;
}
queue<int> q;
dist[k]=0;
q.push(k);
visit[k]=1;
while(!q.empty()){
int now=q.front();
q.pop();
visit[now]=0;
for(i=1;i<=num;i++){
if(dist[now]+f[now][i]<dist[i]){
dist[i]=dist[now]+f[now][i];
if(!visit[i]){
q.push(i);
visit[i]=1;
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////

int main(){
std::ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
///////////////////////////////////////////////////////////////////////
//Add Code:
int n,start,end,u,v,val,i,j;
string a,b;
while(cin>>n,n!=-1){
for(i=1;i<maxn;i++) f[i][i]=0;
for(i=1;i<maxn;i++){
for(j=i+1;j<maxn;j++) f[i][j]=f[j][i]=INF;
}
map<string,int> m;
num=0;
cin>>a>>b;
if(m.find(a)==m.end()) m[a]=++num;
if(m.find(b)==m.end()) m[b]=++num;
start=m[a],end=m[b];
while(n--){
cin>>a>>b>>val;
if(m.find(a)==m.end()) m[a]=++num;
if(m.find(b)==m.end()) m[b]=++num;
u=m[a],v=m[b];
if(val<f[u][v]) f[u][v]=f[v][u]=val;
}
SPFA(start);
if(dist[end]==INF) cout<<-1<<endl;
else cout<<dist[end]<<endl;
}
///////////////////////////////////////////////////////////////////////
return 0;
}

/**************************************************************************
Testcase:
Input:
6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1
Output:
50
**************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: