您的位置:首页 > 其它

zoj 3946 Highway Project【SPFA多个性质的最优化】

2016-04-25 21:53 423 查看
Highway Project

Time Limit: 2 Seconds
Memory Limit: 65536 KB

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.

The Marjar Empire has N cities (including the capital), indexed from 0 to
N - 1 (the capital is 0) and there are M highways can be built. Building the
i-th highway costs Ci dollars. It takes Di minutes to travel between city
Xi and Yi on the i-th highway.

Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city
i (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input

There are multiple test cases. The first line of input contains an integer
T indicating the number of test cases. For each test case:

The first contains two integers N, M (1 ≤ N,
M ≤ 105).

Then followed by M lines, each line contains four integers Xi,
Yi, Di, Ci (0 ≤
Xi, Yi < N, 0 < Di,
Ci < 105).

Output

For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input

2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 1 1
2 3 1 2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 2 1
2 3 1 2

Sample Output

4 3
4 4


Author: Lu, Yi

Source: The 13th Zhejiang Provincial Collegiate Programming Contest

Submit
Status

代码:(注意:用SPFA时边的数组开成边的个数的最大值的2倍)

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
struct Edge
{
LL from,to,val,time,next;
}edge[200005];//双向的边,所以应该是边的总数乘以2!!!wa哭我了!!!
LL head[100005];
LL vis[100005];
LL dis[100005];
LL tm[100005];
LL edgenum;
void addEdge(LL x,LL y,LL z,LL t)
{
Edge E={x,y,z,t,head[x]};
edge[edgenum]=E;
head[x]=edgenum++;
}
void SPFA()
{
queue<LL>q;
q.push(0);
vis[0]=1;//进队列的要进行标记
dis[0]=0;
tm[0]=0;
while(!q.empty())
{
LL u=q.front();
q.pop();
vis[u]=0;//出来的要取消标记!
for(LL i=head[u];i!=-1;i=edge[i].next)
{
LL v=edge[i].to;
if(tm[v]>tm[u]+edge[i].time)
{
tm[v]=tm[u]+edge[i].time;
dis[v]=edge[i].val;//这一点只将v点前面的路的代价赋值给它就行了!
if(!vis[v])//没进队列的将它放进队列,用于更新它周围的点
{
vis[v]=1;
q.push(v);
}
}
else if(tm[v]==tm[u]+edge[i].time)
{
if(dis[v]>edge[i].val)
{
dis[v]=edge[i].val;//这一点只将v点前面的路的代价赋值给它就行了!
}
}
}
}
}
int main()
{
LL T;
scanf("%lld",&T);
while(T--)
{
edgenum=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(LL i=0;i<m;i++)
{
LL x,y,z,t;
scanf("%lld%lld%lld%lld",&x,&y,&t,&z);
addEdge(x,y,z,t);
addEdge(y,x,z,t);
}
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
memset(tm,INF,sizeof(tm));
SPFA();
LL s1=0,s2=0;
for(int i=1;i<n;i++)
{
s1+=tm[i];
s2+=dis[i];
}
printf("%lld %lld\n",s1,s2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: