poj2387 dijsktra模板题
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define PI acos((double)-1)
using namespace std;
typedef long long ll;
const int maxn=2100;
int map[maxn][maxn],t,n,dist[maxn],vis[maxn];
void dijsktra()
{
memset(vis,0,sizeof(vis));
memset(dist,inf,sizeof(dist));
dist[1]=0;
for(int i=1;i<=n;i++)
{
int min=inf,k;
for(int j=1;j<=n;j++)
{
if(dist[j]map[k][j]+min)
dist[j]=map[k][j]+min;
}
}
cout << dist[n] << endl;
}
int main()
{
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=map[j][i]=inf;
}
}
for(int i=0;i
#include
#include
#include
#include
#include
using namespace std;
int map[101][101],n,m;
void floyd()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
if(map[j][i]&&map[i][k])
map[j][k]=1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt=0;
memset(map,0,sizeof(map));
for(int i=0;i
bellman
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define PI acos(double(-1))
using namespace std;
typedef long long ll;
const int maxn=110;
struct node
{
int u,v;
double r,c;
};
node e[maxn*maxn*2];
int n,m,vis[maxn],s,cnt,head[maxn],next[maxn*maxn*2];
double dist[maxn],v;
int bellman()
{
for(int i=1;i<=n;i++)
dist[i]=0;
dist[s]=v;
int flag;
for(int i=0;i
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define PI acos(double(-1))
using namespace std;
typedef long long ll;
const int maxn=3000;
struct node
{
int u,v;
double r,c;
};
node e[maxn];
int n,m,vis[maxn],s,cnt,head[maxn],next[maxn],num[maxn];
double dist[maxn],v;
int spfa()
{
queue q;
for(int i=0;i<=n;i++)
dist[i]=0;
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
dist[s]=v;
q.push(s);
vis[s]=1;
num[s]++;
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=0;
for(int i=head[x];i!=-1;i=next[i])
{
if(dist[e[i].v]<(dist[x]-e[i].c)*e[i].r)
{
dist[e[i].v]=(dist[x]-e[i].c)*e[i].r;
if(!vis[e[i].v])
{
vis[e[i].v]=1;
q.push(e[i].v);
num[e[i].v]++;
}
if(num[e[i].v]>n)
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
{
cnt=0;
memset(head,-1,sizeof(head));
for(int i=0;i