poj 1062 昂贵的聘礼 最短路 dijkstra
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 101 const int inf=0x7fffffff; //无限大 int m,n; int price[maxn][maxn]; int lv[maxn]; int x[maxn]; int dist[maxn]; int visit[maxn]; void init() { memset(price,0,sizeof(price)); memset(lv,0,sizeof(lv)); memset(dist,inf,sizeof(dist)); memset(visit,0,sizeof(visit)); cin>>m>>n; for(int i=1;i<=n;i++) { cin>>price[0][i]>>lv[i]>>x[i]; for(int j=1;j<=x[i];j++) { int t,u; cin>>t>>u; price[t][i]=u; } } } int dijkstra() { int node; int sd; int i,j; for(i=1;i<=n;i++) dist[i]=price[0][i]; for(i=1;i<=n;i++) { node=0; sd=inf; for(j=1;j<=n;j++) { if(!visit[j]&&sd>dist[j]) { sd=dist[j]; node=j; } } if(node==0) break; visit[node]=1; for(j=1;j<=n;j++) { if(!visit[j]&&price[node][j]>0 && dist[j]>dist[node]+price[node][j]) dist[j]=dist[node]+price[node][j]; } } return dist[1]; } int main() { init(); int temp_price; int maxlv; int minprice=inf; for(int i=1;i<=n;i++) { maxlv=lv[i]; for(int j=1;j<=n;j++) { if(lv[j]>maxlv||maxlv-lv[j]>m) visit[j]=1; else visit[j]=0; } temp_price=dijkstra(); if(minprice>temp_price) minprice=temp_price; } cout<<minprice<<endl; return 0; }