您的位置:首页 > 其它

POJ-1797-Heavy Transportation

2013-07-31 18:11 253 查看
题目大意是说给出n个顶点和m条边,每个边都有最大承载量,要求求出从1点运送货物到n点,求能运送货物的最大重量。

其实就是求出1->n的所有路径中最小权值的最大值

用spfa可以做,

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=1e6+100;
const int inf=1<<29;
int e,n,m,pnt[maxn],nxt[maxn],head[1010],cost[maxn],dist[maxn];
bool vis[1010];
queue<int> q;
void AddEdge(int u,int v,int c)
{
pnt[e]=v;nxt[e]=head[u];cost[e]=c;head[u]=e++;
}
int Spfa(int st,int ed)
{
memset(dist,0,sizeof(dist));
memset(vis,0,sizeof(vis));
q.push(st);
vis[st]=1;
dist[st]=inf;
while(!q.empty())
{
int u=q.front();
vis[u]=0;
q.pop();
for(int i=head[u];i!=-1;i=nxt[i])
{
int v=pnt[i];
if(dist[v]<min(dist[u],cost[i]))
{
dist[v]=min(dist[u],cost[i]);
if(!vis[v])
{
q.push(v);
vis[v]=1;
}
}
}
}
return dist[ed];
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
memset(head,-1,sizeof(head));
e=0;
scanf("%d%d",&n,&m);
while(m--)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
AddEdge(u,v,c);
AddEdge(v,u,c);
}
printf("Scenario #%d:\n",cas++);
printf("%d\n",Spfa(1,n));
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: