From Evernote: |
uva 383 |
SPFA 練習
[sourcecode language="cpp"]
//============================================================================
// Name : Page Hopping.cpp
// Date : 2013 2013/2/2 下午5:08:11
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 32
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
map<string,int> sid;
int n,w,qr;
struct node{
int to,w;
node(){to=-1,w=-1;};
node(int to,int w):to(to),w(w){};
};
//node q[900000];
vector<node> edge[M];
int SPFA(int src,int end){
int dis[M];
bool vis[M];
Set(vis,0);
fill(dis,dis+M,oo);
priority_queue<int> q;
q.push(src);
vis[src]=1;
dis[src]=0;
while(!q.empty()){
int u=q.top();q.pop();
FOR(it,edge[u]){
int v=(*it).to,w=(*it).w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
if(!vis[v])vis[v]=1,q.push(v);
}
}
vis[u]=0;
}
if(dis[end]==oo)return 0;
return dis[end]*100;
}
int main() {
ios_base::sync_with_stdio(0);
int test;
char str[10],str2[10];
int ff=0;
scanf("%d",&test);
printf("SHIPPING ROUTES OUTPUT\n");
while(test--){
scanf("%d%d%d",&n,&w,&qr);
for(int i=1;i<=n;i++){
scanf("%s",str);
sid[str]=i;
}
int x,y;
for(int i=0;i<w;i++){
scanf("%s%s",str,str2);
x=sid[str],y=sid[str2];
edge[x].PB(node(y,1));
edge[y].PB(node(x,1));
}
int cost;
printf("\nDATA SET %d\n\n",++ff);
while(qr--){
scanf("%d%s%s",&cost,str,str2);
x=sid[str],y=sid[str2];
cost*=SPFA(x,y);
if(cost)printf("$%d\n",cost);
else printf("NO SHIPMENT POSSIBLE\n");
}
for(int i=0;i<=n;i++)edge[i].clear();
}
printf("\nEND OF OUTPUT\n");
}
[/sourcecode]
留言
張貼留言