HDOJ 1534 Schedule Problem
这里不再写分析,题目分析看这里HDOJ 1534分析
#include <iostream> #include <queue> #include <deque> #include <vector> using namespace std; #define MAXN 1010 #define INF 1000000001 struct edge { int to; int weight; }; vector <edge> adjmap[MAXN]; //邻接表 bool in_queue[MAXN]; //顶点是否在队列中 int in_sum[MAXN]; //顶点入队次数 int dist[MAXN]; //源点到各点的最短路径 int n; //顶点数 int time[MAXN]; bool SPFA(int source) { //queue <int> dq; deque <int> dq; int i,j,x,to; for(i=0;i<=n;i++) { in_sum[i]=0; in_queue[i]=false; dist[i]=-INF; } dq.push_back(source); in_sum[source]++; dist[source]=0; in_queue[source]=true; //初始化完成 while(!dq.empty()) { x=dq.front(); dq.pop_front(); in_queue[x]=false; for(i=0;i<adjmap[x].size();i++) { to=adjmap[x][i].to; if((dist[to] < dist[x]+adjmap[x][i].weight)) { dist[to]=dist[x]+adjmap[x][i].weight; if(!in_queue[to]) { in_queue[to]=true; in_sum[to]++; if(in_sum[to] > n) return false; //dq.push(to); if(!dq.empty()) { if(dist[to] < dist[dq.front()]) dq.push_back(to); else dq.push_front(to); } else dq.push_back(to); } } } } return true; } int main() { char str[4]; int i,j,k,x,y,count=0; edge temp; while(cin>>n) { if(n == 0) break; for(i=1;i<=n;i++) cin>>time[i]; for(i=0;i<=n;i++) adjmap[i].clear(); for(i=0;;i++) { cin>>str; if(str[0] == '#') break; cin>>x>>y; //这里要注意起点和终点 //因为要求的是star[x],所以x要作为有向边的终点 temp.to=x; if(strcmp(str,"FAS")==0) temp.weight=0-time[x]; else if(strcmp(str,"FAF")==0) temp.weight=time[y]-time[x]; else if(strcmp(str,"SAF")==0) temp.weight=time[y]; else temp.weight=0; adjmap[y].push_back(temp); } //增加一个源点,到所有点的权值都为0 //为的是可以从0拓展到所有点,不然会出现错误 for(i=1;i<=n;i++) { temp.to=i; temp.weight=0; adjmap[0].push_back(temp); } if(SPFA(0)) { cout<<"Case "<<++count<<":"<<endl; for(i=1;i<=n;i++) cout<<i<<" "<<dist[i]<<endl; } else cout<<"impossible"<<endl; cout<<endl; } return 0; }
同样只是用SPFA来实现,分析见这里HDOJ 3440 分析
#include <iostream> #include <deque> #include <vector> #include <algorithm> using namespace std; #define INF 1000000000 struct Node { int heigt; int index; }node[1005]; struct edge { int to; int weight; }; vector <edge> adjmap[1005]; bool in_queue[1005]; //顶点是否在队列中 int in_sum[1005]; //顶点入队次数 int dist[1005]; //源点到各点的最短路径 int n; bool cmp(Node &a,Node &b) { return (a.heigt<b.heigt); } bool SPFA(int source) { deque <int> dq; int i,j,x,to; for(i=0;i<n;i++) { in_sum[i]=0; in_queue[i]=false; dist[i]=INF; } dq.push_back(source); in_sum[source]++; in_queue[source]=true; dist[source]=0; while(!dq.empty()) { x=dq.front(); dq.pop_front(); in_queue[x]=false; for(i=0;i<adjmap[x].size();i++) { to=adjmap[x][i].to; if((dist[x] < INF)&&(dist[to] > dist[x]+adjmap[x][i].weight)) { dist[to]=dist[x]+adjmap[x][i].weight; if(!in_queue[to]) { in_queue[to]=true; in_sum[to]++; if(in_sum[to] == n) return false; if(!dq.empty()) { if(dist[to] > dist[dq.front()]) dq.push_back(to); else dq.push_front(to); } else dq.push_back(to); } } } } return true; } int main() { int nCase,count=0,d,i,star,end,s,e; edge temp; cin>>nCase; while(nCase--) { count++; cin>>n>>d; for(i=0;i<n;i++) adjmap[i].clear(); s=INF; e=0; for(i=0;i<n;i++) { cin>>node[i].heigt; node[i].index=i; if(node[i].heigt < s) { s=node[i].heigt; star=i; } if(node[i].heigt > e) { e=node[i].heigt; end=i; } } sort(node,node+n,cmp); for(i=0;i<n-1;i++) { if(node[i].index < node[i+1].index) { temp.to=node[i+1].index; temp.weight=d; adjmap[node[i].index].push_back(temp); } else { temp.to=node[i].index; temp.weight=d; adjmap[node[i+1].index].push_back(temp); } temp.to=i; temp.weight= -1; adjmap[i+1].push_back(temp); } if(star > end) swap(star,end); if(SPFA(star)) cout<<"Case "<<count<<": "<<dist[end]<<endl; else cout<<"Case "<<count<<": "<<-1<<endl; } return 0; }