您的位置:首页 > 其它

PAT 1018. Public Bike Management

2014-04-10 15:16 435 查看
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

// 1018pat2.cpp : 定义控制台应用程序的入口点。
//

#include <iostream>
#include <map>
#include <vector>
#include <math.h>
using namespace std;
const int INF=0x7fffffff;
const int N=501;

struct Edge
{
int next;
int dis;
int cost;
};

struct Edges
{
vector<Edge> list;
}edges
;
map<int,int> cmap;
int dist
;
int cost
;
int parent
;
bool mark
;
int cmax,n,sp,M;

void print_path(int x)
{
if(x==0)
cout<<x<<"->";
else
{
print_path(parent[x]);
if(x==sp)
cout<<x;
else
cout<<x<<"->";
}
}

int main()
{
while(cin>>cmax>>n>>sp>>M)
{
for(int i=0;i<=n;++i)
{
mark[i]=false;
dist[i]=INF;
cost[i]=0;
parent[i]=-1;
edges[i].list.clear();
}
cmap[0]=0;
int tmp;
for(int i=1;i<=n;++i)
{
cin>>tmp;
cmap[i]=tmp;
}
Edge edge;
int a,b,dis;
for(int i=1;i<=M;++i)
{
cin>>a>>b>>dis;
edge.dis=dis;
edge.cost=cmap[b];
edge.next=b;
edges[a].list.push_back(edge);
edge.cost=cmap[a];
edge.next=a;
edges[b].list.push_back(edge);
}
mark[0]=true;
dist[0]=0;
cost[0]=0;
int newP=0;
int next;
int costs;
int d;
for(int i=1;i<=n;++i)
{
if(newP==2)
newP=newP;
for(int j=0;j<edges[newP].list.size();++j)
{
next=edges[newP].list[j].next;
if(mark[next])
continue;
costs=edges[newP].list[j].cost;
d=edges[newP].list[j].dis;
if(dist[next]==-1||(dist[next]>dist[newP]+d)||(dist[next]==dist[newP]+d&&abs(cost[next])>abs(cost[newP]+costs-cmax/2)))
{
dist[next]=dist[newP]+d;
cost[next]=cost[newP]+costs-cmax/2;
parent[next]=newP;
}
}
int min=INF;
for(int i=1;i<=n;++i)
{
if(mark[i]||dist[i]==-1)
continue;
if(dist[i]<min)
{
min=dist[i];
newP=i;
}
}
mark[newP]=true;
}
int res=cost[sp];
if(res<0)
{
cout<<abs(res)<<" ";
print_path(sp);
cout<<" "<<0<<endl;
}
else
{
cout<<0<<" ";
print_path(sp);
cout<<" "<<res<<endl;
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: